// File: /workspace/github_c_makefile_star2_O3/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, %r14
movq %r8, %r15
movl %ecx, -0x4c(%rbp)
movl %edx, %r13d
movq %rsi, -0x58(%rbp)
movq %rdi, %r12
movq 0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x298, %esi            # imm = 0x298
callq 0x11721
movq %rax, %rbx
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x3718a
movq %r14, -0x60(%rbp)
movl $0x298, %edx            # imm = 0x298
movq %rbx, %rdi
xorl %esi, %esi
callq 0x8200
movq %rbx, -0x40(%rbp)
movq %r15, 0x220(%rbx)
movl 0x28(%r12), %eax
testl %eax, %eax
jle 0x37101
xorl %r14d, %r14d
xorl %ebx, %ebx
movq 0x20(%r12), %rcx
movq 0x8(%rcx,%r14), %rdi
testq %rdi, %rdi
je 0x370f2
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
movq %rax, 0x8(%rcx)
movl $0x1, %esi
movl $0x1, %edx
callq 0x1fa4b
testl %eax, %eax
jne 0x371a9
movl 0x28(%r12), %eax
incq %rbx
movslq %eax, %rcx
addq $0x20, %r14
cmpq %rcx, %rbx
jl 0x370bc
movq %r12, %rdi
callq 0x36d92
movq -0x40(%rbp), %rbx
movq %r12, (%rbx)
movl $0x0, 0x1e8(%rbx)
testl %r13d, %r13d
movq %r12, -0x38(%rbp)
movq -0x58(%rbp), %r14
js 0x37195
je 0x37134
movl %r13d, %eax
cmpb $0x0, -0x1(%r14,%rax)
je 0x37195
cmpl %r13d, 0x6c(%r12)
jge 0x371d2
leaq 0x439bc(%rip), %rdx     # 0x7ab02
movl $0x12, %r15d
movq %r12, %rdi
movl $0x12, %esi
xorl %eax, %eax
callq 0xca2d
cmpb $0x0, 0x50(%r12)
je 0x37180
movl $0x7, %r15d
movq %r12, %rdi
movl $0x7, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0xca2d
movb $0x0, 0x50(%r12)
andl 0x48(%r12), %r15d
jmp 0x37529
movl $0x7, %r15d
jmp 0x37529
leaq -0x48(%rbp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1383e
jmp 0x37238
movl %eax, %r15d
movq 0x20(%r12), %rax
movq (%rax,%r14), %rcx
leaq 0x43928(%rip), %rdx     # 0x7aae4
movq %r12, %rdi
movl %r15d, %esi
xorl %eax, %eax
callq 0xca2d
movq -0x40(%rbp), %rbx
jmp 0x37529
testq %r14, %r14
je 0x3722b
leal 0x1(%r13), %esi
movq %r12, %rdi
callq 0x11721
testq %rax, %rax
je 0x3722b
movq %rax, %r15
movl %r13d, %r12d
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x82f0
movb $0x0, (%r15,%r12)
movq -0x38(%rbp), %r12
leaq -0x48(%rbp), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1383e
movq %r12, %rdi
movq %r15, %rsi
callq 0x9ca4
movq 0x230(%rbx), %rax
subq %r15, %rax
jmp 0x3722e
movl %r13d, %eax
addq %r14, %rax
movq %rax, 0x230(%rbx)
cmpb $0x0, 0x50(%r12)
je 0x3724e
addq $0x18, -0x30(%rbp)
movl $0x7, 0x18(%rbx)
jmp 0x37266
movq -0x30(%rbp), %rax
addq $0x18, %rax
cmpl $0x65, 0x18(%rbx)
movq %rax, -0x30(%rbp)
jne 0x37266
movl $0x0, (%rax)
cmpb $0x0, 0x1d(%rbx)
je 0x37320
movq (%rbx), %r15
cmpl $0x0, 0x28(%r15)
jle 0x37320
xorl %r14d, %r14d
xorl %r13d, %r13d
movq 0x20(%r15), %rax
movq 0x8(%rax,%r14), %r12
testq %r12, %r12
je 0x372f5
movb 0x10(%r12), %bl
testb %bl, %bl
jne 0x372ad
movq %r12, %rdi
xorl %esi, %esi
callq 0xd1b2
testl %eax, %eax
jne 0x3730b
movq 0x20(%r15), %rax
movq (%r12), %rcx
movq 0x8(%r12), %rdx
movq %rcx, 0x8(%rdx)
movq 0x18(%rdx), %rcx
movq 0x50(%rcx), %rcx
movl 0x28(%rcx), %ecx
bswapl %ecx
movq 0x18(%rax,%r14), %rax
cmpl (%rax), %ecx
je 0x372e5
movq %r15, %rdi
movl %r13d, %esi
callq 0x2d19c
movq -0x30(%rbp), %rax
movl $0x11, (%rax)
testb %bl, %bl
movq -0x40(%rbp), %rbx
jne 0x372f5
movq %r12, %rdi
callq 0x358e9
incq %r13
movslq 0x28(%r15), %rax
addq $0x20, %r14
cmpq %rax, %r13
jl 0x37284
jmp 0x37320
cmpl $0x7, %eax
movq -0x40(%rbp), %rbx
je 0x3731b
cmpl $0xc0a, %eax            # imm = 0xC0A
jne 0x37320
movb $0x1, 0x50(%r15)
movq -0x38(%rbp), %rax
cmpb $0x0, 0x50(%rax)
movq -0x30(%rbp), %rdx
je 0x37334
movl $0x7, (%rdx)
movq -0x68(%rbp), %rcx
testq %rcx, %rcx
je 0x37347
movq 0x230(%rbx), %rax
movq %rax, (%rcx)
movl (%rdx), %r15d
testl %r15d, %r15d
je 0x373b2
movq -0x38(%rbp), %rax
cmpb $0x0, 0x99(%rax)
jne 0x3748d
movq 0x10(%rbx), %r14
testq %r14, %r14
je 0x3748d
movq -0x58(%rbp), %r13
testq %r13, %r13
je 0x373e1
movq 0x230(%rbx), %rbx
subq %r13, %rbx
movq (%r14), %rdi
leal 0x1(%rbx), %esi
callq 0x11721
testq %rax, %rax
je 0x3745b
movq %rax, %r12
movslq %ebx, %rbx
movq %rax, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x82f0
movb $0x0, (%r12,%rbx)
jmp 0x3745e
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x3734f
movzbl 0x200(%rbx), %eax
testl %eax, %eax
je 0x3734f
cmpl $0x2, %eax
jne 0x373e6
movl $0xc, %eax
movq %rax, -0x30(%rbp)
movl $0x8, %r12d
movl $0x4, %esi
jmp 0x373f7
xorl %r12d, %r12d
jmp 0x37462
movl $0x8, %eax
movq %rax, -0x30(%rbp)
movl $0x8, %esi
xorl %r12d, %r12d
callq 0x3754d
leaq 0x3f6f1(%rip), %rax     # 0x76af4
leaq (%rax,%r12,4), %r13
xorl %r14d, %r14d
movq -0x30(%rbp), %rdx
movq 0x10(%rbx), %rax
movq (%rax), %rcx
cmpb $0x0, 0x50(%rcx)
jne 0x37446
movslq (%r13), %rsi
leaq 0x3f6ce(%rip), %rcx     # 0x76af4
addq %rcx, %rsi
movq 0x20(%rax), %rdi
addq %r14, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0xf0d7
movq -0x30(%rbp), %rdx
incq %r12
addq $0x4, %r13
addq $0x38, %r14
cmpq %rdx, %r12
jb 0x3740e
jmp 0x3734f
xorl %r12d, %r12d
movq -0x40(%rbp), %rbx
movq %r12, 0xe0(%r14)
movzwl 0x9a(%r14), %eax
movl -0x4c(%rbp), %ecx
andl $0x1, %ecx
shll $0xa, %ecx
andl $0xfffffbff, %eax       # imm = 0xFFFFFBFF
orl %ecx, %eax
movw %ax, 0x9a(%r14)
movq -0x38(%rbp), %rax
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x374a8
testl %r15d, %r15d
jne 0x374a1
cmpb $0x0, 0x50(%rax)
je 0x374a8
callq 0xeac6
jmp 0x374af
movq -0x60(%rbp), %rax
movq %rdi, (%rax)
movq -0x48(%rbp), %r12
testq %r12, %r12
je 0x374e4
leaq 0x4221a(%rip), %rdx     # 0x796d9
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
movl %r15d, %esi
movq %r12, %rcx
xorl %eax, %eax
callq 0xca2d
movq %rbx, %rdi
movq -0x40(%rbp), %rbx
movq %r12, %rsi
callq 0x9ca4
jmp 0x374f4
movq -0x38(%rbp), %rdi
movl %r15d, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0xca2d
movq 0x290(%rbx), %rsi
testq %rsi, %rsi
je 0x37525
movq -0x38(%rbp), %r12
movq 0x8(%rsi), %rax
movq %rax, 0x290(%rbx)
movq %r12, %rdi
callq 0x9ca4
movq 0x290(%rbx), %rsi
testq %rsi, %rsi
jne 0x37504
jmp 0x37529
movq -0x38(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x9ca4
movq %r12, %rdi
movl %r15d, %esi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xeaff