// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x240, %rsp            # imm = 0x240
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
movq 0x10(%rbp), %rax
movq $0x0, (%rax)
cmpl $0x0, -0x2c(%rbp)
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rdi
movl $0x38, %esi
callq 0x3c9520
movq %rax, -0x50(%rbp)
leaq -0x1f8(%rbp), %rdi
xorl %esi, %esi
movl $0x1a8, %edx            # imm = 0x1A8
callq 0x19360
cmpq $0x0, -0x50(%rbp)
jne 0x3cae34
jmp 0x3cb3fd
movq -0x8(%rbp), %rax
movq %rax, -0x1f8(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3c00a0
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
callq 0x3bfd40
movq -0x18(%rbp), %rdx
movq -0x10(%rbp), %rcx
leaq -0x1f8(%rbp), %rdi
xorl %esi, %esi
callq 0x3cb4b0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x3caea9
movq -0x48(%rbp), %rax
cmpl $0x0, 0x54(%rax)
je 0x3caea9
movq $0x0, -0x48(%rbp)
movq -0x18(%rbp), %rdx
leaq -0x1f8(%rbp), %rdi
leaq 0x1dfeee(%rip), %rsi    # 0x5aad90
movb $0x0, %al
callq 0x3cb660
cmpq $0x0, -0x48(%rbp)
je 0x3caee2
movq -0x48(%rbp), %rax
movl 0x40(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x3caee2
movq $0x0, -0x48(%rbp)
movq -0x18(%rbp), %rdx
leaq -0x1f8(%rbp), %rdi
leaq 0x1dfed3(%rip), %rsi    # 0x5aadae
movb $0x0, %al
callq 0x3cb660
cmpq $0x0, -0x48(%rbp)
je 0x3caf15
movq -0x48(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x3caf15
movq $0x0, -0x48(%rbp)
movq -0x18(%rbp), %rdx
leaq -0x1f8(%rbp), %rdi
leaq 0x1dfec4(%rip), %rsi    # 0x5aadd2
movb $0x0, %al
callq 0x3cb660
cmpq $0x0, -0x48(%rbp)
jne 0x3caf55
cmpq $0x0, -0x1f0(%rbp)
je 0x3caf49
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3c00a0
movq -0x1f0(%rbp), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x1f0(%rbp)
movl $0x1, -0x38(%rbp)
jmp 0x3cb3fd
movq -0x48(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x238(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x70(%rax), %rsi
callq 0x3cb770
movl %eax, %ecx
movq -0x238(%rbp), %rax
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x28(%rax)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x48(%rbp), %rcx
movswl 0x46(%rcx), %ecx
cmpl %ecx, %eax
jge 0x3cafe8
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x34(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x3c3e60
cmpl $0x0, %eax
jne 0x3cafdb
jmp 0x3cafe8
jmp 0x3cafdd
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x3cafa6
movl -0x34(%rbp), %eax
movq -0x48(%rbp), %rcx
movswl 0x46(%rcx), %ecx
cmpl %ecx, %eax
jne 0x3cb02a
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3c00a0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x1dfdd4(%rip), %rsi    # 0x5aade7
movb $0x0, %al
callq 0x3cb7d0
movq %rax, -0x40(%rbp)
movl $0x1, -0x38(%rbp)
jmp 0x3cb3fd
cmpl $0x0, -0x2c(%rbp)
je 0x3cb1d3
movq $0x0, -0x200(%rbp)
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
andq $0x4000, %rax           # imm = 0x4000
cmpq $0x0, %rax
je 0x3cb0e5
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x210(%rbp)
cmpq $0x0, -0x210(%rbp)
je 0x3cb0e3
movl $0x0, -0x214(%rbp)
movl -0x214(%rbp), %eax
movq -0x210(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jge 0x3cb0cd
movq -0x210(%rbp), %rax
addq $0x40, %rax
movslq -0x214(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl -0x34(%rbp), %eax
jne 0x3cb0ba
leaq 0x1dfd49(%rip), %rax    # 0x5aadfc
movq %rax, -0x200(%rbp)
jmp 0x3cb0bc
movl -0x214(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x214(%rbp)
jmp 0x3cb07a
jmp 0x3cb0cf
movq -0x210(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x210(%rbp)
jmp 0x3cb066
jmp 0x3cb0e5
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x208(%rbp)
cmpq $0x0, -0x208(%rbp)
je 0x3cb191
movl $0x0, -0x218(%rbp)
movl -0x218(%rbp), %eax
movq -0x208(%rbp), %rcx
movzwl 0x5e(%rcx), %ecx
cmpl %ecx, %eax
jge 0x3cb178
movq -0x208(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x218(%rbp), %rcx
movswl (%rax,%rcx,2), %eax
cmpl -0x34(%rbp), %eax
je 0x3cb157
movq -0x208(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x218(%rbp), %rcx
movswl (%rax,%rcx,2), %eax
cmpl $-0x2, %eax
jne 0x3cb165
leaq 0x1e25af(%rip), %rax    # 0x5ad70d
movq %rax, -0x200(%rbp)
jmp 0x3cb167
movl -0x218(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x218(%rbp)
jmp 0x3cb10c
jmp 0x3cb17a
movq -0x208(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x208(%rbp)
jmp 0x3cb0f4
cmpq $0x0, -0x200(%rbp)
je 0x3cb1d1
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3c00a0
movq -0x8(%rbp), %rdi
movq -0x200(%rbp), %rdx
leaq 0x1dfc4e(%rip), %rsi    # 0x5aae08
movb $0x0, %al
callq 0x3cb7d0
movq %rax, -0x40(%rbp)
movl $0x1, -0x38(%rbp)
jmp 0x3cb3fd
jmp 0x3cb1d3
leaq -0x1f8(%rbp), %rdi
callq 0x3cb880
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x50(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x3cb390
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x220(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x70(%rax), %rsi
callq 0x3cb770
movl %eax, -0x224(%rbp)
movq -0x220(%rbp), %rdi
movl -0x224(%rbp), %edx
movl -0x2c(%rbp), %ecx
movq -0x48(%rbp), %rax
movq 0x70(%rax), %rax
movl (%rax), %r8d
movq -0x48(%rbp), %rax
movq 0x70(%rax), %rax
movl 0x4(%rax), %r9d
movl $0x2, %esi
callq 0x3cb970
movq -0x220(%rbp), %rdi
movl $0x1, %esi
callq 0x3cb9f0
movq -0x220(%rbp), %rdi
movl $0x6, %esi
leaq 0x1db93c(%rip), %rdx    # 0x5a6bb0
xorl %ecx, %ecx
callq 0x3cba40
movq %rax, -0x230(%rbp)
movq -0x220(%rbp), %rdi
movl -0x224(%rbp), %esi
callq 0x3cbbb0
movq -0x8(%rbp), %rax
movzbl 0x61(%rax), %eax
cmpl $0x0, %eax
jne 0x3cb2e9
movl -0x224(%rbp), %ecx
movq -0x230(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x48(%rbp), %rax
movl 0x38(%rax), %ecx
movq -0x230(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x2c(%rbp), %ecx
movq -0x230(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x220(%rbp), %rdi
movq -0x48(%rbp), %rax
movq (%rax), %rdx
movl $0x2, %esi
xorl %ecx, %ecx
callq 0x3cbc30
movq -0x8(%rbp), %rax
movzbl 0x61(%rax), %eax
cmpl $0x0, %eax
jne 0x3cb38e
cmpl $0x0, -0x2c(%rbp)
je 0x3cb30b
movq -0x230(%rbp), %rax
movb $0x61, 0x18(%rax)
movq -0x48(%rbp), %rax
movl 0x38(%rax), %ecx
movq -0x230(%rbp), %rax
movl %ecx, 0x20(%rax)
movl -0x224(%rbp), %ecx
movq -0x230(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x230(%rbp), %rax
movb $-0x3, 0x19(%rax)
movq -0x48(%rbp), %rax
movswl 0x46(%rax), %ecx
addl $0x1, %ecx
movq -0x230(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x48(%rbp), %rax
movswl 0x46(%rax), %ecx
movq -0x230(%rbp), %rax
movl %ecx, 0x50(%rax)
movw $0x0, -0xe0(%rbp)
movl $0x1, -0x1c0(%rbp)
movl $0x1, -0x1c4(%rbp)
movq -0x220(%rbp), %rdi
leaq -0x1f8(%rbp), %rsi
callq 0x3cbd30
jmp 0x3cb390
movl -0x34(%rbp), %eax
movw %ax, %cx
movq -0x50(%rbp), %rax
movw %cx, 0x8(%rax)
movq -0x8(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rax
cmpb $0x0, 0x61(%rax)
je 0x3cb3b6
jmp 0x3cb3fd
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x3cc160
movl %eax, -0x38(%rbp)
movl -0x30(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x30(%rbp)
xorl %eax, %eax
cmpl $0x32, %ecx
movb %al, -0x239(%rbp)
jge 0x3cb3ed
cmpl $0x11, -0x38(%rbp)
sete %al
movb %al, -0x239(%rbp)
movb -0x239(%rbp), %al
testb $0x1, %al
jne 0x3cae15
jmp 0x3cb3fd
cmpl $0x0, -0x38(%rbp)
jne 0x3cb41d
movq -0x8(%rbp), %rax
movzbl 0x61(%rax), %eax
cmpl $0x0, %eax
jne 0x3cb41d
movq -0x50(%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
jmp 0x3cb449
cmpq $0x0, -0x50(%rbp)
je 0x3cb43c
movq -0x50(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x3cb43c
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x3c7330
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x3c00a0
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %esi
movq -0x40(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
leaq 0x1d0bc2(%rip), %rax    # 0x59c021
cmpq $0x0, %rcx
cmovneq %rax, %rdx
movq -0x40(%rbp), %rcx
movb $0x0, %al
callq 0x3c50b0
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3c00a0
leaq -0x1f8(%rbp), %rdi
callq 0x3cc3b0
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %esi
callq 0x3c7380
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq
nopw %cs:(%rax,%rax)