// File: /workspace/github_c_makefile_star2_O0/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
subq $0x80, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x8(%rbp), %rdi
movl $0x298, %esi            # imm = 0x298
callq 0x11ca0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x4b9f9
movl $0x7, -0x3c(%rbp)
jmp 0x4bde4
movq -0x20(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x220(%rax)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jge 0x4ba99
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x4ba89
movq -0x48(%rbp), %rdi
callq 0x4be10
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0x4ba87
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movl -0x3c(%rbp), %esi
movq -0x50(%rbp), %rcx
leaq 0x63b65(%rip), %rdx     # 0xaf5e0
movb $0x0, %al
callq 0xd720
jmp 0x4bde4
jmp 0x4ba89
jmp 0x4ba8b
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x4ba0f
movq -0x8(%rbp), %rdi
callq 0x4b240
movq -0x8(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movl $0x0, 0x1e8(%rax)
cmpl $0x0, -0x14(%rbp)
jl 0x4bb9b
cmpl $0x0, -0x14(%rbp)
je 0x4bae5
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x4bb9b
movq -0x8(%rbp), %rax
movl 0x6c(%rax), %eax
movl %eax, -0x5c(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jle 0x4bb24
movq -0x8(%rbp), %rdi
movl $0x12, %esi
leaq 0x63af7(%rip), %rdx     # 0xaf5fe
movb $0x0, %al
callq 0xd720
movq -0x8(%rbp), %rdi
movl $0x12, %esi
callq 0xfe10
movl %eax, -0x3c(%rbp)
jmp 0x4bde4
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
callq 0x4a7a0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x4bb83
movq -0x30(%rbp), %rdi
movq -0x58(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x16360
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x9cd0
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rax
movq 0x230(%rax), %rax
movq -0x58(%rbp), %rdx
subq %rdx, %rax
addq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x230(%rax)
jmp 0x4bb99
movq -0x10(%rbp), %rcx
movslq -0x14(%rbp), %rax
addq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x230(%rax)
jmp 0x4bbac
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x16360
movq -0x8(%rbp), %rax
cmpb $0x0, 0x50(%rax)
je 0x4bbc1
movq -0x30(%rbp), %rax
movl $0x7, 0x18(%rax)
movq -0x30(%rbp), %rax
cmpl $0x65, 0x18(%rax)
jne 0x4bbd6
movq -0x30(%rbp), %rax
movl $0x0, 0x18(%rax)
movq -0x30(%rbp), %rax
cmpb $0x0, 0x1d(%rax)
je 0x4bbe9
movq -0x30(%rbp), %rdi
callq 0x4be50
movq -0x8(%rbp), %rax
cmpb $0x0, 0x50(%rax)
je 0x4bbfe
movq -0x30(%rbp), %rax
movl $0x7, 0x18(%rax)
cmpq $0x0, 0x10(%rbp)
je 0x4bc17
movq -0x30(%rbp), %rax
movq 0x230(%rax), %rcx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x4bce4
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x4bce4
movq -0x30(%rbp), %rax
movzbl 0x200(%rax), %eax
cmpl $0x0, %eax
je 0x4bce4
movq -0x30(%rbp), %rax
movzbl 0x200(%rax), %eax
cmpl $0x2, %eax
jne 0x4bc80
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movl $0x4, %esi
callq 0x4bf60
movl $0x8, -0x60(%rbp)
movl $0xc, -0x64(%rbp)
jmp 0x4bca0
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movl $0x8, %esi
callq 0x4bf60
movl $0x0, -0x60(%rbp)
movl $0x8, -0x64(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x64(%rbp), %eax
jge 0x4bce2
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x40(%rbp), %esi
subl -0x60(%rbp), %esi
movslq -0x40(%rbp), %rcx
leaq 0x837b9(%rip), %rax     # 0xcf480
movq (%rax,%rcx,8), %rcx
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %r8d
callq 0x4c030
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x4bca6
jmp 0x4bce4
movq -0x8(%rbp), %rax
movzbl 0x99(%rax), %eax
cmpl $0x0, %eax
jne 0x4bd24
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x30(%rbp), %rax
movq 0x230(%rax), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
movl %eax, %edx
movl -0x18(%rbp), %ecx
callq 0x4c0c0
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x4bd51
cmpl $0x0, -0x3c(%rbp)
jne 0x4bd42
movq -0x8(%rbp), %rax
movzbl 0x50(%rax), %eax
cmpl $0x0, %eax
je 0x4bd51
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0xfdc0
jmp 0x4bd60
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
cmpq $0x0, -0x38(%rbp)
je 0x4bd8f
movq -0x8(%rbp), %rdi
movl -0x3c(%rbp), %esi
movq -0x38(%rbp), %rcx
leaq 0x62457(%rip), %rdx     # 0xae1d0
movb $0x0, %al
callq 0xd720
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x9cd0
jmp 0x4bda1
movq -0x8(%rbp), %rdi
movl -0x3c(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
movb $0x0, %al
callq 0xd720
jmp 0x4bda3
movq -0x30(%rbp), %rax
cmpq $0x0, 0x290(%rax)
je 0x4bde2
movq -0x30(%rbp), %rax
movq 0x290(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x290(%rax)
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x9cd0
jmp 0x4bda3
jmp 0x4bde4
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x9cd0
movq -0x8(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0xfe10
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)