// File: /workspace/github_c_makefile_star2_O1/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, %r13
movl %ecx, %r15d
movq %rdx, -0x48(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdi, %rbx
movq 0x10(%rbp), %rax
movq %rax, -0x70(%rbp)
movq $0x0, -0x50(%rbp)
movl $0x298, %esi            # imm = 0x298
callq 0x115e9
movq %rax, %r12
testq %rax, %rax
je 0x36aa0
movl %r15d, -0x54(%rbp)
movq %r14, -0x68(%rbp)
xorl %r15d, %r15d
movl $0x298, %edx            # imm = 0x298
movq %r12, %rdi
xorl %esi, %esi
callq 0x8200
movq %r12, -0x30(%rbp)
movq %r13, 0x220(%r12)
cmpl $0x0, 0x28(%rbx)
movq %rbx, -0x38(%rbp)
jle 0x36a37
xorl %r14d, %r14d
leaq 0x44120(%rip), %r13     # 0x7aae4
xorl %r12d, %r12d
xorl %r15d, %r15d
movq 0x20(%rbx), %rax
movq 0x8(%rax,%r14), %rdi
movb $0x1, %bl
testq %rdi, %rdi
je 0x36a1b
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
movq %rax, 0x8(%rcx)
movl $0x1, %esi
movl $0x1, %edx
callq 0x1fc86
testl %eax, %eax
je 0x36a18
movl %eax, %r15d
movq -0x38(%rbp), %rdi
movq 0x20(%rdi), %rax
movq (%rax,%r14), %rcx
xorl %ebx, %ebx
movl %r15d, %esi
movq %r13, %rdx
xorl %eax, %eax
callq 0xc8f4
jmp 0x36a1b
xorl %r15d, %r15d
testb %bl, %bl
je 0x36abc
incq %r12
movq -0x38(%rbp), %rbx
movslq 0x28(%rbx), %rax
addq $0x20, %r14
cmpq %rax, %r12
jl 0x369ca
movq %rbx, %rdi
callq 0x3668e
movq -0x30(%rbp), %r12
movq %rbx, (%r12)
movl $0x0, 0x1e8(%r12)
movq -0x48(%rbp), %r13
testl %r13d, %r13d
movq -0x40(%rbp), %rsi
js 0x36aab
je 0x36a6c
movl %r13d, %eax
cmpb $0x0, -0x1(%rsi,%rax)
je 0x36aab
movl 0x6c(%rbx), %r14d
cmpl %r13d, %r14d
jge 0x36ac9
leaq 0x44086(%rip), %rdx     # 0x7ab02
movq %rbx, %rdi
movl $0x12, %esi
xorl %eax, %eax
callq 0xc8f4
movq %rbx, %rdi
movl $0x12, %esi
callq 0xea22
movl %eax, %r15d
jmp 0x36b55
movl $0x7, %r15d
jmp 0x36b5a
leaq -0x50(%rbp), %rdx
movq %r12, %rdi
callq 0x1373c
jmp 0x36b7e
movq -0x38(%rbp), %rbx
movq -0x30(%rbp), %r12
jmp 0x36b5a
testq %rsi, %rsi
je 0x36b03
leal 0x1(%r13), %esi
movq %rbx, %rdi
callq 0x115e9
movq %r13, %rcx
movq %rax, %r13
testq %rax, %rax
je 0x36b06
movl %ecx, %edx
movq %rdx, -0x60(%rbp)
movq %r13, %rdi
movq -0x40(%rbp), %rsi
callq 0x82f0
movq -0x60(%rbp), %rax
movb $0x0, (%r13,%rax)
jmp 0x36b06
xorl %r13d, %r13d
testq %r13, %r13
je 0x36b42
leaq -0x50(%rbp), %rdx
movq %r12, %rdi
movq %r13, %rsi
callq 0x1373c
movq %rbx, %rdi
movq %r13, %rsi
callq 0x9c3a
movq 0x230(%r12), %rax
subq %r13, %rax
addq -0x40(%rbp), %rax
movq %rax, 0x230(%r12)
movq -0x48(%rbp), %r13
jmp 0x36b55
movq -0x48(%rbp), %r13
movl %r13d, %eax
addq -0x40(%rbp), %rax
movq %rax, 0x230(%r12)
cmpl %r13d, %r14d
jge 0x36b7e
movq %rbx, %rdi
movq %r12, %rsi
callq 0x9c3a
movq %rbx, %rdi
movl %r15d, %esi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xea22
cmpb $0x0, 0x50(%rbx)
je 0x36b8d
movl $0x7, 0x18(%r12)
cmpl $0x65, 0x18(%r12)
jne 0x36b9e
movl $0x0, 0x18(%r12)
cmpb $0x0, 0x1d(%r12)
je 0x36c75
movq -0x30(%rbp), %rax
movq (%rax), %r15
cmpl $0x0, 0x28(%r15)
jle 0x36c75
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x20(%r15), %rax
movq 0x8(%rax,%r14), %r13
testq %r13, %r13
je 0x36c28
movb 0x10(%r13), %dl
testb %dl, %dl
je 0x36c44
movq (%r13), %rax
movq 0x8(%r13), %rcx
movq %rax, 0x8(%rcx)
movq 0x18(%rcx), %rax
movq 0x50(%rax), %rax
movl 0x28(%rax), %eax
bswapl %eax
movq 0x20(%r15), %rcx
movq 0x18(%rcx,%r14), %rcx
cmpl (%rcx), %eax
je 0x36c18
movq %r15, %rdi
movl %r12d, %esi
movl %edx, %ebx
callq 0x2c895
movl %ebx, %edx
movq -0x30(%rbp), %rax
movl $0x11, 0x18(%rax)
xorl %ebx, %ebx
testb %dl, %dl
jne 0x36c2d
movq %r13, %rdi
callq 0x351a8
jmp 0x36c2d
movl $0x4, %ebx
testb $0x3, %bl
jne 0x36c75
incq %r12
movslq 0x28(%r15), %rax
addq $0x20, %r14
cmpq %rax, %r12
jl 0x36bc2
jmp 0x36c75
movq %r13, %rdi
xorl %esi, %esi
movl %edx, %ebx
callq 0xd067
movl %ebx, %edx
testl %eax, %eax
je 0x36bd8
movl $0x1, %ebx
cmpl $0xc0a, %eax            # imm = 0xC0A
je 0x36c67
cmpl $0x7, %eax
jne 0x36c2d
movb $0x1, 0x50(%r15)
testl %eax, %eax
jne 0x36c2d
jmp 0x36bd8
movq -0x38(%rbp), %rax
cmpb $0x0, 0x50(%rax)
movq -0x30(%rbp), %rcx
je 0x36c8a
movl $0x7, 0x18(%rcx)
movq -0x70(%rbp), %rdx
testq %rdx, %rdx
je 0x36c9d
movq 0x230(%rcx), %rax
movq %rax, (%rdx)
movl 0x18(%rcx), %r15d
testl %r15d, %r15d
je 0x36d0a
movq -0x38(%rbp), %rbx
cmpb $0x0, 0x99(%rbx)
movq -0x30(%rbp), %rax
jne 0x36d73
movq 0x10(%rax), %r14
testq %r14, %r14
je 0x36d73
movq -0x40(%rbp), %r13
testq %r13, %r13
je 0x36d45
movq 0x230(%rax), %rbx
subq %r13, %rbx
movq (%r14), %rdi
leal 0x1(%rbx), %esi
callq 0x115e9
movq %rax, %r12
testq %rax, %rax
je 0x36d04
movq %r13, %rsi
movslq %ebx, %r13
movq %r12, %rdi
movq %r13, %rdx
callq 0x82f0
movb $0x0, (%r12,%r13)
movq -0x38(%rbp), %rbx
jmp 0x36d48
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
testq %rdi, %rdi
je 0x36ca6
movq -0x30(%rbp), %rax
movzbl 0x200(%rax), %eax
testl %eax, %eax
je 0x36ca6
cmpl $0x2, %eax
jne 0x36dfc
movl $0xc, %r14d
movl $0x8, %r12d
movl $0x4, %esi
jmp 0x36e0a
xorl %r12d, %r12d
movq %r12, 0xe0(%r14)
movzwl 0x9a(%r14), %eax
movl -0x54(%rbp), %ecx
andl $0x1, %ecx
shll $0xa, %ecx
andl $0xfffffbff, %eax       # imm = 0xFFFFFBFF
orl %ecx, %eax
movw %ax, 0x9a(%r14)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
testq %rdi, %rdi
je 0x36d8e
testl %r15d, %r15d
jne 0x36d87
cmpb $0x0, 0x50(%rbx)
je 0x36d8e
callq 0xe9e9
jmp 0x36d95
movq -0x68(%rbp), %rax
movq %rdi, (%rax)
movq -0x50(%rbp), %r12
testq %r12, %r12
je 0x36dc2
leaq 0x42934(%rip), %rdx     # 0x796d9
movq %rbx, %rdi
movl %r15d, %esi
movq %r12, %rcx
xorl %eax, %eax
callq 0xc8f4
movq %rbx, %rdi
movq %r12, %rsi
callq 0x9c3a
jmp 0x36dd1
movq %rbx, %rdi
movl %r15d, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0xc8f4
movq -0x30(%rbp), %r12
movq 0x290(%r12), %rsi
testq %rsi, %rsi
je 0x36b5a
movq 0x8(%rsi), %rax
movq %rax, 0x290(%r12)
movq %rbx, %rdi
callq 0x9c3a
jmp 0x36dd5
movl $0x8, %r14d
movl $0x8, %esi
xorl %r12d, %r12d
callq 0x36e69
leaq 0x3fc8e(%rip), %rax     # 0x76aa4
leaq (%rax,%r12,4), %r13
xorl %ebx, %ebx
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rcx
cmpb $0x0, 0x50(%rcx)
jne 0x36e54
movslq (%r13), %rsi
leaq 0x3fc6c(%rip), %rcx     # 0x76aa4
addq %rcx, %rsi
movq 0x20(%rax), %rdi
addq %rbx, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0xeff4
incq %r12
addq $0x4, %r13
addq $0x38, %rbx
cmpq %r14, %r12
jb 0x36e1c
jmp 0x36ca6