// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3Prepare
static int sqlite3Prepare(
  sqlite3 *db,              /* Database handle. */
  const char *zSql,         /* UTF-8 encoded SQL statement. */
  int nBytes,               /* Length of zSql in bytes. */
  int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
  Vdbe *pReprepare,         /* VM being reprepared */
  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
  const char **pzTail       /* OUT: End of parsed string */
){
  Parse *pParse;            /* Parsing context */
  char *zErrMsg = 0;        /* Error message */
  int rc = SQLITE_OK;       /* Result code */
  int i;                    /* Loop counter */

  /* Allocate the parsing context */
  pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
  if( pParse==0 ){
    rc = SQLITE_NOMEM;
    goto end_prepare;
  }
  pParse->pReprepare = pReprepare;
  assert( ppStmt && *ppStmt==0 );
  assert( !db->mallocFailed );
  assert( sqlite3_mutex_held(db->mutex) );

  /* Check to verify that it is possible to get a read lock on all
  ** database schemas.  The inability to get a read lock indicates that
  ** some other database connection is holding a write-lock, which in
  ** turn means that the other connection has made uncommitted changes
  ** to the schema.
  **
  ** Were we to proceed and prepare the statement against the uncommitted
  ** schema changes and if those schema changes are subsequently rolled
  ** back and different changes are made in their place, then when this
  ** prepared statement goes to run the schema cookie would fail to detect
  ** the schema change.  Disaster would follow.
  **
  ** This thread is currently holding mutexes on all Btrees (because
  ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
  ** is not possible for another thread to start a new schema change
  ** while this routine is running.  Hence, we do not need to hold 
  ** locks on the schema, we just need to make sure nobody else is 
  ** holding them.
  **
  ** Note that setting READ_UNCOMMITTED overrides most lock detection,
  ** but it does *not* override schema lock detection, so this all still
  ** works even if READ_UNCOMMITTED is set.
  */
  for(i=0; i<db->nDb; i++) {
    Btree *pBt = db->aDb[i].pBt;
    if( pBt ){
      assert( sqlite3BtreeHoldsMutex(pBt) );
      rc = sqlite3BtreeSchemaLocked(pBt);
      if( rc ){
        const char *zDb = db->aDb[i].zName;
        sqlite3Error(db, rc, "database schema is locked: %s", zDb);
        testcase( db->flags & SQLITE_ReadUncommitted );
        goto end_prepare;
      }
    }
  }

  sqlite3VtabUnlockList(db);

  pParse->db = db;
  pParse->nQueryLoop = 0;  /* Logarithmic, so 0 really means 1 */
  if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
    char *zSqlCopy;
    int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
    testcase( nBytes==mxLen );
    testcase( nBytes==mxLen+1 );
    if( nBytes>mxLen ){
      sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
      rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
      goto end_prepare;
    }
    zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
    if( zSqlCopy ){
      sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
      sqlite3DbFree(db, zSqlCopy);
      pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
    }else{
      pParse->zTail = &zSql[nBytes];
    }
  }else{
    sqlite3RunParser(pParse, zSql, &zErrMsg);
  }
  assert( 0==pParse->nQueryLoop );

  if( db->mallocFailed ){
    pParse->rc = SQLITE_NOMEM;
  }
  if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
  if( pParse->checkSchema ){
    schemaIsValid(pParse);
  }
  if( db->mallocFailed ){
    pParse->rc = SQLITE_NOMEM;
  }
  if( pzTail ){
    *pzTail = pParse->zTail;
  }
  rc = pParse->rc;

#ifndef SQLITE_OMIT_EXPLAIN
  if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
    static const char * const azColName[] = {
       "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
       "selectid", "order", "from", "detail"
    };
    int iFirst, mx;
    if( pParse->explain==2 ){
      sqlite3VdbeSetNumCols(pParse->pVdbe, 4);
      iFirst = 8;
      mx = 12;
    }else{
      sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
      iFirst = 0;
      mx = 8;
    }
    for(i=iFirst; i<mx; i++){
      sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
                            azColName[i], SQLITE_STATIC);
    }
  }
#endif

  if( db->init.busy==0 ){
    Vdbe *pVdbe = pParse->pVdbe;
    sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
  }
  if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
    sqlite3VdbeFinalize(pParse->pVdbe);
    assert(!(*ppStmt));
  }else{
    *ppStmt = (sqlite3_stmt*)pParse->pVdbe;
  }

  if( zErrMsg ){
    sqlite3Error(db, rc, "%s", zErrMsg);
    sqlite3DbFree(db, zErrMsg);
  }else{
    sqlite3Error(db, rc, 0);
  }

  /* Delete any TriggerPrg structures allocated while parsing this statement. */
  while( pParse->pTriggerPrg ){
    TriggerPrg *pT = pParse->pTriggerPrg;
    pParse->pTriggerPrg = pT->pNext;
    sqlite3DbFree(db, pT);
  }

end_prepare:

  sqlite3StackFree(db, pParse);
  rc = sqlite3ApiExit(db, rc);
  assert( (rc&db->errMask)==rc );
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %rbx
movq %r8, %r15
movl %ecx, -0x5c(%rbp)
movl %edx, %r13d
movq %rsi, -0x48(%rbp)
movq %rdi, %r12
movq 0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
andq $0x0, -0x58(%rbp)
movl $0x298, %esi            # imm = 0x298
callq 0xf4ae
movq %rax, %r14
movq %rax, -0x40(%rbp)
testq %rax, %rax
movq %r12, -0x30(%rbp)
je 0x31a5f
movq %rbx, -0x68(%rbp)
movq %r14, -0x38(%rbp)
movq %r15, 0x220(%r14)
xorl %r14d, %r14d
pushq $0x1
popq %r15
xorl %ebx, %ebx
movslq 0x28(%r12), %rax
cmpq %rax, %rbx
jge 0x31a01
movq 0x20(%r12), %rax
movq 0x8(%rax,%r14), %rdi
testq %rdi, %rdi
je 0x319f4
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
movq %rax, 0x8(%rcx)
movl %r15d, %esi
movl %r15d, %edx
callq 0x1d0ea
testl %eax, %eax
jne 0x31a68
incq %rbx
addq $0x20, %r14
movq -0x30(%rbp), %r12
jmp 0x319c1
movq %r12, %rdi
callq 0x316c4
movq -0x38(%rbp), %r14
movq %r12, (%r14)
andl $0x0, 0x1e8(%r14)
testl %r13d, %r13d
movq -0x48(%rbp), %rsi
js 0x31a91
movl %r13d, %ebx
je 0x31a2d
cmpb $0x0, -0x1(%rsi,%rbx)
je 0x31a91
cmpl %r13d, 0x6c(%r12)
jge 0x31a9f
leaq 0x3b0e7(%rip), %rdx     # 0x6cb22
pushq $0x12
popq %r15
movq %r12, %rdi
movl %r15d, %esi
xorl %eax, %eax
callq 0xbdb1
movq %r12, %rdi
movl %r15d, %esi
callq 0xdc59
movl %eax, %r12d
jmp 0x31d15
pushq $0x7
popq %r12
jmp 0x31d15
movl %eax, %r12d
movq -0x30(%rbp), %rdi
movq 0x20(%rdi), %rax
movq (%rax,%r14), %rcx
leaq 0x3b086(%rip), %rdx     # 0x6cb04
movl %r12d, %esi
xorl %eax, %eax
callq 0xbdb1
movq -0x38(%rbp), %r14
jmp 0x31d15
leaq -0x58(%rbp), %rdx
movq %r14, %rdi
callq 0x1211e
jmp 0x31aee
movq %r12, %rdi
movl %r13d, %edx
callq 0x310c8
testq %rax, %rax
je 0x31ae3
movq %rax, %r15
leaq -0x58(%rbp), %rdx
movq %r14, %rdi
movq %rax, %rsi
callq 0x1211e
movq %r12, %rdi
movq %r15, %rsi
callq 0x968b
movq 0x230(%r14), %rax
subq %r15, %rax
addq -0x48(%rbp), %rax
movq %rax, 0x230(%r14)
jmp 0x31aee
addq -0x48(%rbp), %rbx
movq %rbx, 0x230(%r14)
movb 0x50(%r12), %al
testb %al, %al
je 0x31b0d
movq -0x40(%rbp), %rdx
addq $0x18, %rdx
movl $0x7, 0x18(%r14)
movq -0x50(%rbp), %rcx
jmp 0x31b23
movq -0x40(%rbp), %rdx
addq $0x18, %rdx
cmpl $0x65, 0x18(%r14)
movq -0x50(%rbp), %rcx
jne 0x31b23
andl $0x0, (%rdx)
cmpb $0x0, 0x1d(%r14)
je 0x31bdd
movq %rdx, -0x40(%rbp)
movq (%r14), %r12
xorl %r14d, %r14d
xorl %r13d, %r13d
movslq 0x28(%r12), %rax
cmpq %rax, %r13
jge 0x31bca
movq 0x20(%r12), %rax
movq 0x8(%rax,%r14), %r15
testq %r15, %r15
je 0x31baf
movb 0x10(%r15), %bl
testb %bl, %bl
jne 0x31b6e
movq %r15, %rdi
xorl %esi, %esi
callq 0xc487
testl %eax, %eax
jne 0x31bb8
movq %r15, %rdi
pushq $0x1
popq %rsi
leaq -0x60(%rbp), %rdx
callq 0xca45
movl -0x60(%rbp), %eax
movq 0x20(%r12), %rcx
movq 0x18(%rcx,%r14), %rcx
cmpl (%rcx), %eax
je 0x31ba3
movq %r12, %rdi
movl %r13d, %esi
callq 0x2a189
movq -0x40(%rbp), %rax
movl $0x11, (%rax)
testb %bl, %bl
jne 0x31baf
movq %r15, %rdi
callq 0x30487
incq %r13
addq $0x20, %r14
jmp 0x31b3b
cmpl $0xc0a, %eax            # imm = 0xC0A
je 0x31bc4
cmpl $0x7, %eax
jne 0x31bca
movb $0x1, 0x50(%r12)
movq -0x30(%rbp), %rax
movb 0x50(%rax), %al
movq -0x38(%rbp), %r14
movq -0x50(%rbp), %rcx
movq -0x40(%rbp), %rdx
testb %al, %al
je 0x31be7
movl $0x7, (%rdx)
testq %rcx, %rcx
je 0x31bf6
movq 0x230(%r14), %rax
movq %rax, (%rcx)
movl (%rdx), %r12d
testl %r12d, %r12d
je 0x31cad
movq -0x30(%rbp), %rax
cmpb $0x0, 0x99(%rax)
movq -0x38(%rbp), %r14
jne 0x31c56
movq 0x10(%r14), %rbx
testq %rbx, %rbx
je 0x31c56
movl 0x230(%r14), %edx
movq -0x48(%rbp), %rsi
subl %esi, %edx
movq (%rbx), %rdi
callq 0x310c8
movq %rax, 0xe0(%rbx)
movzwl 0x9a(%rbx), %eax
movl -0x5c(%rbp), %ecx
andl $0x1, %ecx
shll $0xa, %ecx
andl $0xfffffbff, %eax       # imm = 0xFFFFFBFF
orl %ecx, %eax
movw %ax, 0x9a(%rbx)
movq 0x10(%r14), %rdi
testq %rdi, %rdi
je 0x31c75
testl %r12d, %r12d
jne 0x31c6e
movq -0x30(%rbp), %rax
cmpb $0x0, 0x50(%rax)
je 0x31c75
callq 0xdc20
jmp 0x31c7c
movq -0x68(%rbp), %rax
movq %rdi, (%rax)
movq -0x58(%rbp), %r15
testq %r15, %r15
je 0x31ce3
leaq 0x39a6d(%rip), %rdx     # 0x6b6f9
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movl %r12d, %esi
movq %r15, %rcx
xorl %eax, %eax
callq 0xbdb1
movq %rbx, %rdi
movq %r15, %rsi
callq 0x968b
jmp 0x31d09
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdi
testq %rdi, %rdi
je 0x31c02
movq -0x38(%rbp), %rax
movzbl 0x200(%rax), %eax
testl %eax, %eax
je 0x31c02
cmpl $0x2, %eax
jne 0x31d3d
pushq $0xc
popq %r14
pushq $0x8
popq %r15
pushq $0x4
popq %rsi
jmp 0x31d47
movq -0x30(%rbp), %rdi
movl %r12d, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0xbdb1
jmp 0x31d09
movq 0x8(%rsi), %rax
movq %rax, 0x290(%r14)
movq -0x30(%rbp), %rdi
callq 0x968b
movq 0x290(%r14), %rsi
testq %rsi, %rsi
jne 0x31cf5
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x968b
movq %rbx, %rdi
movl %r12d, %esi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xdc59
pushq $0x8
popq %r14
pushq $0x8
popq %rsi
xorl %r15d, %r15d
callq 0x31d90
leal (,%r15,4), %ebx
subq %r15, %r14
xorl %r15d, %r15d
leaq 0x36d53(%rip), %r13     # 0x68ab4
cmpq %r15, %r14
je 0x31c02
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdi
leaq (%rbx,%r15,4), %rax
movslq (%r13,%rax), %rcx
addq %r13, %rcx
movl %r15d, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x31dfc
incq %r15
jmp 0x31d61