// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/sqlite-3.33.0/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. */
  u32 prepFlags,            /* Zero or more SQLITE_PREPARE_* flags */
  Vdbe *pReprepare,         /* VM being reprepared */
  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
  const char **pzTail       /* OUT: End of parsed string */
){
  char *zErrMsg = 0;        /* Error message */
  int rc = SQLITE_OK;       /* Result code */
  int i;                    /* Loop counter */
  Parse sParse;             /* Parsing context */

  memset(&sParse, 0, PARSE_HDR_SZ);
  memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ);
  sParse.pReprepare = pReprepare;
  assert( ppStmt && *ppStmt==0 );
  /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
  assert( sqlite3_mutex_held(db->mutex) );

  /* For a long-term use prepared statement avoid the use of
  ** lookaside memory.
  */
  if( prepFlags & SQLITE_PREPARE_PERSISTENT ){
    sParse.disableLookaside++;
    DisableLookaside;
  }
  sParse.disableVtab = (prepFlags & SQLITE_PREPARE_NO_VTAB)!=0;

  /* 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.
  */
  if( !db->noSharedCache ){
    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].zDbSName;
          sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
          testcase( db->flags & SQLITE_ReadUncommit );
          goto end_prepare;
        }
      }
    }
  }

  sqlite3VtabUnlockList(db);

  sParse.db = db;
  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 ){
      sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long");
      rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
      goto end_prepare;
    }
    zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
    if( zSqlCopy ){
      sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
      sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
      sqlite3DbFree(db, zSqlCopy);
    }else{
      sParse.zTail = &zSql[nBytes];
    }
  }else{
    sqlite3RunParser(&sParse, zSql, &zErrMsg);
  }
  assert( 0==sParse.nQueryLoop );

  if( sParse.rc==SQLITE_DONE ){
    sParse.rc = SQLITE_OK;
  }
  if( sParse.checkSchema ){
    schemaIsValid(&sParse);
  }
  if( pzTail ){
    *pzTail = sParse.zTail;
  }

  if( db->init.busy==0 ){
    sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
  }
  if( db->mallocFailed ){
    sParse.rc = SQLITE_NOMEM_BKPT;
  }
  rc = sParse.rc;
  if( rc!=SQLITE_OK ){
    if( sParse.pVdbe ) sqlite3VdbeFinalize(sParse.pVdbe);
    assert(!(*ppStmt));
  }else{
    *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
  }

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

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

end_prepare:

  sqlite3ParserReset(&sParse);
  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %r9, 0x28(%rsp)
movq %r8, %r14
movl %ecx, %ebp
movl %edx, %r13d
movq %rsi, 0x10(%rsp)
movq %rdi, %rbx
andq $0x0, 0x18(%rsp)
leaq 0x30(%rsp), %rdi
movl $0xd4, %edx
xorl %esi, %esi
callq 0x17380
leaq 0x138(%rsp), %r15
movl $0xa0, %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x17380
movq %r14, 0x30(%r15)
movl %ebp, 0xc(%rsp)
testb $0x1, %bpl
je 0x24ad59
movb $0x1, 0x54(%rsp)
incl 0x170(%rbx)
andw $0x0, 0x174(%rbx)
movl 0xc(%rsp), %eax
shrb $0x2, %al
andb $0x1, %al
movb %al, 0x55(%rsp)
cmpb $0x0, 0x69(%rbx)
je 0x24ade1
movq %rbx, %rdi
callq 0x24b12d
movq %rbx, 0x30(%rsp)
testl %r13d, %r13d
js 0x24adc5
movl %r13d, %r14d
je 0x24ad90
movq 0x10(%rsp), %rax
cmpb $0x0, -0x1(%rax,%r14)
je 0x24adc5
cmpl %r13d, 0x80(%rbx)
jge 0x24ae26
leaq 0x12bdd4(%rip), %rdx    # 0x376b78
pushq $0x12
popq %rbp
movq %rbx, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x2226f3
movq %rbx, %rdi
movl %ebp, %esi
callq 0x22417d
movl %eax, %r12d
jmp 0x24b06a
leaq 0x30(%rsp), %rdi
leaq 0x18(%rsp), %rdx
movq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0x228e55
jmp 0x24aea6
xorl %r14d, %r14d
pushq $0x1
popq %rbp
xorl %r15d, %r15d
movslq 0x28(%rbx), %rax
cmpq %rax, %r15
jge 0x24ad6c
movq 0x20(%rbx), %rax
movq 0x8(%rax,%r14), %rdi
testq %rdi, %rdi
je 0x24ae1d
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
movq %rax, 0x8(%rcx)
movl %ebp, %esi
movl %ebp, %edx
callq 0x235d90
testl %eax, %eax
jne 0x24ae77
incq %r15
addq $0x20, %r14
jmp 0x24adea
movq %rbx, %rdi
movq 0x10(%rsp), %r12
movq %r12, %rsi
movq %r14, %rdx
callq 0x24b168
testq %rax, %rax
je 0x24ae9b
movq %rax, %r15
leaq 0x30(%rsp), %r14
leaq 0x18(%rsp), %rdx
movq %r14, %rdi
movq %rax, %rsi
callq 0x228e55
movq 0x140(%r14), %rax
subq %r15, %rax
addq %r12, %rax
movq %rax, 0x140(%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x22d0fc
jmp 0x24aea6
movl %eax, %r12d
movq 0x20(%rbx), %rax
movq (%rax,%r14), %rcx
leaq 0x12bcd1(%rip), %rdx    # 0x376b5a
movq %rbx, %rdi
movl %r12d, %esi
xorl %eax, %eax
callq 0x2226f3
jmp 0x24b06a
addq %r12, %r14
movq %r14, 0x170(%rsp)
cmpl $0x65, 0x48(%rsp)
jne 0x24aeb2
andl $0x0, 0x48(%rsp)
movq 0x210(%rsp), %rcx
cmpb $0x0, 0x4d(%rsp)
je 0x24af75
movq 0x30(%rsp), %r12
xorl %r13d, %r13d
xorl %r15d, %r15d
movslq 0x28(%r12), %rax
cmpq %rax, %r15
jge 0x24af68
movq 0x20(%r12), %rax
movq 0x8(%rax,%r13), %rbp
testq %rbp, %rbp
je 0x24af48
movb 0x10(%rbp), %r14b
testb %r14b, %r14b
jne 0x24af06
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x222db8
testl %eax, %eax
jne 0x24af54
movq %rbp, %rdi
pushq $0x1
popq %rsi
leaq 0x24(%rsp), %rdx
callq 0x242986
movl 0x24(%rsp), %eax
movq 0x20(%r12), %rcx
movq 0x18(%rcx,%r13), %rcx
cmpl (%rcx), %eax
je 0x24af3b
movq %r12, %rdi
movl %r15d, %esi
callq 0x242921
movl $0x11, 0x48(%rsp)
testb %r14b, %r14b
jne 0x24af48
movq %rbp, %rdi
callq 0x24a7c9
incq %r15
addq $0x20, %r13
jmp 0x24aed0
cmpl $0xc0a, %eax            # imm = 0xC0A
je 0x24af60
cmpl $0x7, %eax
jne 0x24af68
movq %r12, %rdi
callq 0x224bb1
movq 0x10(%rsp), %r12
movq 0x210(%rsp), %rcx
testq %rcx, %rcx
je 0x24af85
movq 0x170(%rsp), %rax
movq %rax, (%rcx)
cmpb $0x0, 0xb5(%rbx)
jne 0x24afcf
movq 0x40(%rsp), %r14
testq %r14, %r14
je 0x24afcf
movq 0x170(%rsp), %rax
subq %r12, %rax
movl 0xc(%rsp), %ecx
movb %cl, 0xc4(%r14)
testb %cl, %cl
js 0x24afba
andl $0x0, 0x114(%r14)
movq (%r14), %rdi
movslq %eax, %rdx
movq %r12, %rsi
callq 0x24b168
movq %rax, 0xf0(%r14)
cmpb $0x0, 0x61(%rbx)
je 0x24afe3
movl $0x7, 0x48(%rsp)
pushq $0x7
popq %r12
jmp 0x24afed
movl 0x48(%rsp), %r12d
testl %r12d, %r12d
je 0x24affe
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x24b00e
callq 0x224149
jmp 0x24b00e
movq 0x40(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rax, (%rcx)
xorl %r12d, %r12d
movq 0x18(%rsp), %r14
testq %r14, %r14
je 0x24b03c
leaq 0x11de8a(%rip), %rdx    # 0x368ea9
movq %rbx, %rdi
movl %r12d, %esi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2226f3
movq %rbx, %rdi
movq %r14, %rsi
callq 0x22d0fc
jmp 0x24b05d
movq %rbx, %rdi
movl %r12d, %esi
callq 0x222800
jmp 0x24b05d
movq 0x8(%rsi), %rax
movq %rax, 0x1b8(%rsp)
movq %rbx, %rdi
callq 0x22d0fc
movq 0x1b8(%rsp), %rsi
testq %rsi, %rsi
jne 0x24b049
leaq 0x30(%rsp), %rdi
callq 0x22767e
movl %r12d, %eax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq