// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/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 flags,              /* 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 */

  /* 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 VdbeOpList openBlob[] = {
    {OP_Transaction, 0, 0, 0},     /* 0: Start a transaction */
    {OP_VerifyCookie, 0, 0, 0},    /* 1: Check the schema cookie */
    {OP_TableLock, 0, 0, 0},       /* 2: Acquire a read or write lock */

    /* One of the following two instructions is replaced by an OP_Noop. */
    {OP_OpenRead, 0, 0, 0},        /* 3: Open cursor 0 for reading */
    {OP_OpenWrite, 0, 0, 0},       /* 4: Open cursor 0 for read/write */

    {OP_Variable, 1, 1, 1},        /* 5: Push the rowid to the stack */
    {OP_NotExists, 0, 10, 1},      /* 6: Seek the cursor */
    {OP_Column, 0, 0, 1},          /* 7  */
    {OP_ResultRow, 1, 0, 0},       /* 8  */
    {OP_Goto, 0, 5, 0},            /* 9  */
    {OP_Close, 0, 0, 0},           /* 10 */
    {OP_Halt, 0, 0, 0},            /* 11 */
  };

  int rc = SQLITE_OK;
  char *zErr = 0;
  Table *pTab;
  Parse *pParse = 0;
  Incrblob *pBlob = 0;

  flags = !!flags;                /* flags = (flags ? 1 : 0); */
  *ppBlob = 0;

  sqlite3_mutex_enter(db->mutex);

  pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
  if( !pBlob ) goto blob_open_out;
  pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
  if( !pParse ) goto blob_open_out;

  do {
    memset(pParse, 0, sizeof(Parse));
    pParse->db = db;
    sqlite3DbFree(db, zErr);
    zErr = 0;

    sqlite3BtreeEnterAll(db);
    pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
    if( pTab && IsVirtual(pTab) ){
      pTab = 0;
      sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
    }
#ifndef SQLITE_OMIT_VIEW
    if( pTab && pTab->pSelect ){
      pTab = 0;
      sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
    }
#endif
    if( !pTab ){
      if( pParse->zErrMsg ){
        sqlite3DbFree(db, zErr);
        zErr = pParse->zErrMsg;
        pParse->zErrMsg = 0;
      }
      rc = SQLITE_ERROR;
      sqlite3BtreeLeaveAll(db);
      goto blob_open_out;
    }

    /* 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. 
    ** It is against the rules to open a column to which either of these
    ** descriptions applies for writing.  */
    if( flags ){
      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->nColumn; j++){
          if( pIdx->aiColumn[j]==iCol ){
            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(db);
    assert( pBlob->pStmt || db->mallocFailed );
    if( pBlob->pStmt ){
      Vdbe *v = (Vdbe *)pBlob->pStmt;
      int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);

      sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);


      /* Configure the OP_Transaction */
      sqlite3VdbeChangeP1(v, 0, iDb);
      sqlite3VdbeChangeP2(v, 0, flags);

      /* Configure the OP_VerifyCookie */
      sqlite3VdbeChangeP1(v, 1, iDb);
      sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
      sqlite3VdbeChangeP3(v, 1, pTab->pSchema->iGeneration);

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

      /* Configure the OP_TableLock instruction */
#ifdef SQLITE_OMIT_SHARED_CACHE
      sqlite3VdbeChangeToNoop(v, 2);
#else
      sqlite3VdbeChangeP1(v, 2, iDb);
      sqlite3VdbeChangeP2(v, 2, pTab->tnum);
      sqlite3VdbeChangeP3(v, 2, flags);
      sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
#endif

      /* Remove either the OP_OpenWrite or OpenRead. Set the P2 
      ** parameter of the other to pTab->tnum.  */
      sqlite3VdbeChangeToNoop(v, 4 - flags);
      sqlite3VdbeChangeP2(v, 3 + flags, pTab->tnum);
      sqlite3VdbeChangeP3(v, 3 + flags, 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.
      */
      sqlite3VdbeChangeP4(v, 3+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
      sqlite3VdbeChangeP2(v, 7, pTab->nCol);
      if( !db->mallocFailed ){
        pParse->nVar = 1;
        pParse->nMem = 1;
        pParse->nTab = 1;
        sqlite3VdbeMakeReady(v, pParse);
      }
    }
   
    pBlob->flags = flags;
    pBlob->iCol = iCol;
    pBlob->db = db;
    sqlite3BtreeLeaveAll(db);
    if( db->mallocFailed ){
      goto blob_open_out;
    }
    sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
    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);
  }
  sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
  sqlite3DbFree(db, zErr);
  sqlite3StackFree(db, pParse);
  rc = sqlite3ApiExit(db, rc);
  sqlite3_mutex_leave(db->mutex);
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
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 $0x0, -0x58(%rbp)
cmpl $0x0, -0x2c(%rbp)
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x2c(%rbp)
movq 0x10(%rbp), %rax
movq $0x0, (%rax)
movq -0x8(%rbp), %rdi
movl $0x28, %esi
callq 0x11ca0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x12f09
jmp 0x13432
movq -0x8(%rbp), %rdi
movl $0x298, %esi            # imm = 0x298
callq 0x134e0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x12f27
jmp 0x13432
jmp 0x12f29
movq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x298, %edx            # imm = 0x298
callq 0x8200
movq -0x8(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x9cd0
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
callq 0x9890
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x10(%rbp), %rcx
xorl %esi, %esi
callq 0x13620
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x12fae
movq -0x48(%rbp), %rax
movzbl 0x46(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x12fae
movq $0x0, -0x48(%rbp)
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x9afe6(%rip), %rsi     # 0xadf8d
movb $0x0, %al
callq 0x136f0
cmpq $0x0, -0x48(%rbp)
je 0x12fde
movq -0x48(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x12fde
movq $0x0, -0x48(%rbp)
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x9afd4(%rip), %rsi     # 0xadfab
movb $0x0, %al
callq 0x136f0
cmpq $0x0, -0x48(%rbp)
jne 0x13021
movq -0x50(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x13015
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x9cd0
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x50(%rbp), %rax
movq $0x0, 0x8(%rax)
movl $0x1, -0x38(%rbp)
jmp 0x13432
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x48(%rbp), %rcx
movswl 0x42(%rcx), %ecx
cmpl %ecx, %eax
jge 0x1306a
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x34(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0xc970
cmpl $0x0, %eax
jne 0x1305d
jmp 0x1306a
jmp 0x1305f
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x13028
movl -0x34(%rbp), %eax
movq -0x48(%rbp), %rcx
movswl 0x42(%rcx), %ecx
cmpl %ecx, %eax
jne 0x130ac
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x9cd0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x9af2b(%rip), %rsi     # 0xadfc0
movb $0x0, %al
callq 0x137f0
movq %rax, -0x40(%rbp)
movl $0x1, -0x38(%rbp)
jmp 0x13432
cmpl $0x0, -0x2c(%rbp)
je 0x131da
movq $0x0, -0x60(%rbp)
movq -0x8(%rbp), %rax
movl 0x2c(%rax), %eax
andl $0x80000, %eax          # imm = 0x80000
cmpl $0x0, %eax
je 0x13139
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
je 0x13137
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
movq -0x70(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jge 0x13127
movq -0x70(%rbp), %rax
addq $0x40, %rax
movslq -0x74(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl -0x34(%rbp), %eax
jne 0x1311a
leaq 0x9aebf(%rip), %rax     # 0xadfd5
movq %rax, -0x60(%rbp)
jmp 0x1311c
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x130e9
jmp 0x13129
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x70(%rbp)
jmp 0x130db
jmp 0x13139
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x1319e
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
movq -0x68(%rbp), %rcx
movzwl 0x54(%rcx), %ecx
cmpl %ecx, %eax
jge 0x1318e
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x78(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x34(%rbp), %eax
jne 0x13181
leaq 0x9ce09(%rip), %rax     # 0xaff86
movq %rax, -0x60(%rbp)
jmp 0x13183
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x13153
jmp 0x13190
movq -0x68(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x68(%rbp)
jmp 0x13145
cmpq $0x0, -0x60(%rbp)
je 0x131d8
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x9cd0
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rdx
leaq 0x9ae20(%rip), %rsi     # 0xadfe1
movb $0x0, %al
callq 0x137f0
movq %rax, -0x40(%rbp)
movl $0x1, -0x38(%rbp)
jmp 0x13432
jmp 0x131da
movq -0x8(%rbp), %rdi
callq 0x138a0
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x58(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x133aa
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x68(%rax), %rsi
callq 0x13940
movl %eax, -0x84(%rbp)
movq -0x80(%rbp), %rdi
movl $0xc, %esi
leaq 0x97a10(%rip), %rdx     # 0xaac40
callq 0x139a0
movq -0x80(%rbp), %rdi
movl -0x84(%rbp), %edx
xorl %esi, %esi
callq 0x13b00
movq -0x80(%rbp), %rdi
movl -0x2c(%rbp), %edx
xorl %esi, %esi
callq 0x13b40
movq -0x80(%rbp), %rdi
movl -0x84(%rbp), %edx
movl $0x1, %esi
callq 0x13b00
movq -0x80(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x68(%rax), %rax
movl (%rax), %edx
movl $0x1, %esi
callq 0x13b40
movq -0x80(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x68(%rax), %rax
movl 0x4(%rax), %edx
movl $0x1, %esi
callq 0x13b80
movq -0x80(%rbp), %rdi
movl -0x84(%rbp), %esi
callq 0x13bc0
movq -0x80(%rbp), %rdi
movl -0x84(%rbp), %edx
movl $0x2, %esi
callq 0x13b00
movq -0x80(%rbp), %rdi
movq -0x48(%rbp), %rax
movl 0x3c(%rax), %edx
movl $0x2, %esi
callq 0x13b40
movq -0x80(%rbp), %rdi
movl -0x2c(%rbp), %edx
movl $0x2, %esi
callq 0x13b80
movq -0x80(%rbp), %rdi
movq -0x48(%rbp), %rax
movq (%rax), %rdx
movl $0x2, %esi
xorl %ecx, %ecx
callq 0x13c20
movq -0x80(%rbp), %rdi
movl $0x4, %esi
subl -0x2c(%rbp), %esi
callq 0x13e60
movq -0x80(%rbp), %rdi
movl -0x2c(%rbp), %esi
addl $0x3, %esi
movq -0x48(%rbp), %rax
movl 0x3c(%rax), %edx
callq 0x13b40
movq -0x80(%rbp), %rdi
movl -0x2c(%rbp), %esi
addl $0x3, %esi
movl -0x84(%rbp), %edx
callq 0x13b80
movq -0x80(%rbp), %rdi
movl -0x2c(%rbp), %esi
addl $0x3, %esi
movq -0x48(%rbp), %rax
movswl 0x42(%rax), %eax
addl $0x1, %eax
movslq %eax, %rdx
movl $0xfffffff2, %ecx       # imm = 0xFFFFFFF2
callq 0x13c20
movq -0x80(%rbp), %rdi
movq -0x48(%rbp), %rax
movswl 0x42(%rax), %edx
movl $0x7, %esi
callq 0x13b40
movq -0x8(%rbp), %rax
cmpb $0x0, 0x50(%rax)
jne 0x133a8
movq -0x50(%rbp), %rax
movl $0x1, 0x1f8(%rax)
movq -0x50(%rbp), %rax
movl $0x1, 0x58(%rax)
movq -0x50(%rbp), %rax
movl $0x1, 0x54(%rax)
movq -0x80(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x13ee0
jmp 0x133aa
movl -0x2c(%rbp), %ecx
movq -0x58(%rbp), %rax
movl %ecx, (%rax)
movl -0x34(%rbp), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x8(%rbp), %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rax
cmpb $0x0, 0x50(%rax)
je 0x133d5
jmp 0x13432
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rdx
movl $0x1, %esi
callq 0x125e0
movq -0x58(%rbp), %rdi
movq -0x28(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x14370
movl %eax, -0x38(%rbp)
movl -0x30(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x30(%rbp)
xorl %eax, %eax
cmpl $0x32, %ecx
movb %al, -0x85(%rbp)
jge 0x13422
cmpl $0x11, -0x38(%rbp)
sete %al
movb %al, -0x85(%rbp)
movb -0x85(%rbp), %al
testb $0x1, %al
jne 0x12f29
jmp 0x13432
cmpl $0x0, -0x38(%rbp)
jne 0x13452
movq -0x8(%rbp), %rax
movzbl 0x50(%rax), %eax
cmpl $0x0, %eax
jne 0x13452
movq -0x58(%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
jmp 0x1347e
cmpq $0x0, -0x58(%rbp)
je 0x13471
movq -0x58(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x13471
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0xfdc0
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x9cd0
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %esi
movq -0x40(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
leaq 0x9ad3c(%rip), %rax     # 0xae1d0
cmpq $0x0, %rcx
cmovneq %rax, %rdx
movq -0x40(%rbp), %rcx
movb $0x0, %al
callq 0xd720
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x9cd0
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x9cd0
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %esi
callq 0xfe10
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)