// File: /workspace/github_c_makefile_star2_O3/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 $0x1c8, %rsp            # imm = 0x1C8
movq %r9, 0x18(%rsp)
movq %r8, %r14
movl %ecx, %ebp
movl %edx, %r13d
movq %rsi, 0x10(%rsp)
movq %rdi, %rbx
movq $0x0, (%rsp)
leaq 0x20(%rsp), %rdi
movl $0xd4, %edx
xorl %esi, %esi
callq 0x173a0
leaq 0x128(%rsp), %r12
movl $0xa0, %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x173a0
movq %r14, 0x30(%r12)
testb $0x1, %bpl
je 0x2a5785
movb $0x1, 0x44(%rsp)
incl 0x170(%rbx)
movw $0x0, 0x174(%rbx)
movl %ebp, 0xc(%rsp)
movl %ebp, %eax
shrb $0x2, %al
andb $0x1, %al
movb %al, 0x45(%rsp)
cmpb $0x0, 0x69(%rbx)
je 0x2a5813
movq %rbx, %rdi
callq 0x2a5baa
movq %rbx, 0x20(%rsp)
testl %r13d, %r13d
movq 0x10(%rsp), %r15
js 0x2a57fe
movl %r13d, %r14d
je 0x2a57be
cmpb $0x0, -0x1(%r15,%r14)
je 0x2a57fe
cmpl %r13d, 0x80(%rbx)
jge 0x2a5867
leaq 0x130f86(%rip), %rdx    # 0x3d6758
movq %rbx, %rdi
movl $0x12, %esi
xorl %eax, %eax
callq 0x2762da
cmpb $0x0, 0x61(%rbx)
je 0x2a5ae1
movq %rbx, %rdi
callq 0x28fd56
movl $0x7, %r12d
jmp 0x2a5b0c
leaq 0x20(%rsp), %rdi
movq %rsp, %rdx
movq %r15, %rsi
callq 0x27daa4
jmp 0x2a58d2
movl 0x28(%rbx), %eax
testl %eax, %eax
jle 0x2a579a
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x20(%rbx), %rcx
movq 0x8(%rcx,%r14), %rdi
testq %rdi, %rdi
je 0x2a5853
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
movq %rax, 0x8(%rcx)
movl $0x1, %esi
movl $0x1, %edx
callq 0x28c8fe
testl %eax, %eax
jne 0x2a5ac0
movl 0x28(%rbx), %eax
incq %r15
movslq %eax, %rcx
addq $0x20, %r14
cmpq %rcx, %r15
jl 0x2a5820
jmp 0x2a579a
testq %r15, %r15
je 0x2a58c7
leaq 0x1(%r14), %rsi
movq %rbx, %rdi
callq 0x282a35
testq %rax, %rax
je 0x2a58c7
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x17660
movb $0x0, (%r12,%r14)
leaq 0x20(%rsp), %r14
movq %rsp, %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0x27daa4
movq 0x140(%r14), %rax
subq %r12, %rax
addq %r15, %rax
movq %rax, 0x140(%r14)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x282904
jmp 0x2a58d2
addq %r15, %r14
movq %r14, 0x160(%rsp)
cmpl $0x65, 0x38(%rsp)
jne 0x2a58e1
movl $0x0, 0x38(%rsp)
cmpb $0x0, 0x3d(%rsp)
je 0x2a5998
movq 0x20(%rsp), %r12
cmpl $0x0, 0x28(%r12)
jle 0x2a5998
xorl %r15d, %r15d
xorl %r13d, %r13d
movq 0x20(%r12), %rax
movq 0x8(%rax,%r15), %r14
testq %r14, %r14
je 0x2a5971
movb 0x10(%r14), %bpl
testb %bpl, %bpl
jne 0x2a5930
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x276a19
testl %eax, %eax
jne 0x2a5984
movq 0x20(%r12), %rax
movq (%r14), %rcx
movq 0x8(%r14), %rdx
movq %rcx, 0x8(%rdx)
movq 0x18(%rdx), %rcx
movq 0x50(%rcx), %rcx
movl 0x28(%rcx), %ecx
bswapl %ecx
movq 0x18(%rax,%r15), %rax
cmpl (%rax), %ecx
je 0x2a5964
movq %r12, %rdi
movl %r13d, %esi
callq 0x29bacb
movl $0x11, 0x38(%rsp)
testb %bpl, %bpl
jne 0x2a5971
movq %r14, %rdi
callq 0x2a50e4
incq %r13
movslq 0x28(%r12), %rax
addq $0x20, %r15
cmpq %rax, %r13
jl 0x2a5903
jmp 0x2a5998
cmpl $0x7, %eax
je 0x2a5990
cmpl $0xc0a, %eax            # imm = 0xC0A
jne 0x2a5998
movq %r12, %rdi
callq 0x278b1c
movq 0x200(%rsp), %rcx
testq %rcx, %rcx
je 0x2a59b0
movq 0x160(%rsp), %rax
movq %rax, (%rcx)
cmpb $0x0, 0xb5(%rbx)
jne 0x2a5a29
movq 0x30(%rsp), %r15
testq %r15, %r15
je 0x2a5a29
movq 0x160(%rsp), %rax
movl 0xc(%rsp), %ecx
movb %cl, 0xc4(%r15)
testb %cl, %cl
js 0x2a59e5
movl $0x0, 0x114(%r15)
movq 0x10(%rsp), %r13
testq %r13, %r13
je 0x2a5a1f
subq %r13, %rax
movslq %eax, %r12
movq (%r15), %rdi
leaq 0x1(%r12), %rsi
callq 0x282a35
testq %rax, %rax
je 0x2a5a1f
movq %rax, %r14
movq %rax, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x17660
movb $0x0, (%r14,%r12)
jmp 0x2a5a22
xorl %r14d, %r14d
movq %r14, 0xf0(%r15)
cmpb $0x0, 0x61(%rbx)
je 0x2a5a3f
movl $0x7, 0x38(%rsp)
movl $0x7, %r12d
jmp 0x2a5a49
movl 0x38(%rsp), %r12d
testl %r12d, %r12d
je 0x2a5a91
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x2a5a58
callq 0x277f75
movq (%rsp), %r14
testq %r14, %r14
je 0x2a5a80
leaq 0x123041(%rip), %rdx    # 0x3c8aa9
movq %rbx, %rdi
movl %r12d, %esi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2762da
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x2a5afa
movl %r12d, 0x50(%rbx)
movq %rbx, %rdi
movl %r12d, %esi
callq 0x284b1f
jmp 0x2a5aff
movq 0x30(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movq (%rsp), %r14
xorl %r12d, %r12d
testq %r14, %r14
jne 0x2a5a61
movl $0x0, 0x50(%rbx)
xorl %r12d, %r12d
cmpq $0x0, 0x160(%rbx)
jne 0x2a5a84
jmp 0x2a5aff
movl %eax, %r12d
movq 0x20(%rbx), %rax
movq (%rax,%r14), %rcx
leaq 0x130c68(%rip), %rdx    # 0x3d673a
movq %rbx, %rdi
movl %r12d, %esi
xorl %eax, %eax
callq 0x2762da
jmp 0x2a5b0c
movl 0x54(%rbx), %r12d
andl $0x12, %r12d
jmp 0x2a5b0c
movq 0x8(%rsi), %rax
movq %rax, 0x1a8(%rsp)
movq %rbx, %rdi
callq 0x282904
movq 0x1a8(%rsp), %rsi
testq %rsi, %rsi
jne 0x2a5aeb
leaq 0x20(%rsp), %rdi
callq 0x27bd9d
movl %r12d, %eax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq