// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: sqlite3AlterRenameTable
SQLITE_PRIVATE void sqlite3AlterRenameTable(
  Parse *pParse,            /* Parser context. */
  SrcList *pSrc,            /* The table to rename. */
  Token *pName              /* The new table name. */
){
  int iDb;                  /* Database that contains the table */
  char *zDb;                /* Name of database iDb */
  Table *pTab;              /* Table being renamed */
  char *zName = 0;          /* NULL-terminated version of pName */
  sqlite3 *db = pParse->db; /* Database connection */
  int nTabName;             /* Number of UTF-8 characters in zTabName */
  const char *zTabName;     /* Original name of the table */
  Vdbe *v;
  VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
  u32 savedDbFlags;         /* Saved value of db->mDbFlags */

  savedDbFlags = db->mDbFlags;
  if( NEVER(db->mallocFailed) ) goto exit_rename_table;
  assert( pSrc->nSrc==1 );
  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );

  pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
  if( !pTab ) goto exit_rename_table;
  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
  zDb = db->aDb[iDb].zDbSName;
  db->mDbFlags |= DBFLAG_PreferBuiltin;

  /* Get a NULL terminated version of the new table name. */
  zName = sqlite3NameFromToken(db, pName);
  if( !zName ) goto exit_rename_table;

  /* Check that a table or index named 'zName' does not already exist
  ** in database iDb. If so, this is an error.
  */
  if( sqlite3FindTable(db, zName, zDb)
   || sqlite3FindIndex(db, zName, zDb)
   || sqlite3IsShadowTableOf(db, pTab, zName)
  ){
    sqlite3ErrorMsg(pParse,
        "there is already another table or index with this name: %s", zName);
    goto exit_rename_table;
  }

  /* Make sure it is not a system table being altered, or a reserved name
  ** that the table is being renamed to.
  */
  if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
    goto exit_rename_table;
  }
  if( SQLITE_OK!=sqlite3CheckObjectName(pParse,zName,"table",zName) ){
    goto exit_rename_table;
  }

#ifndef SQLITE_OMIT_VIEW
  if( pTab->pSelect ){
    sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
    goto exit_rename_table;
  }
#endif

#ifndef SQLITE_OMIT_AUTHORIZATION
  /* Invoke the authorization callback. */
  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
    goto exit_rename_table;
  }
#endif

#ifndef SQLITE_OMIT_VIRTUALTABLE
  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
    goto exit_rename_table;
  }
  if( IsVirtual(pTab) ){
    pVTab = sqlite3GetVTable(db, pTab);
    if( pVTab->pVtab->pModule->xRename==0 ){
      pVTab = 0;
    }
  }
#endif

  /* Begin a transaction for database iDb. Then modify the schema cookie
  ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(),
  ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the
  ** nested SQL may raise an exception.  */
  v = sqlite3GetVdbe(pParse);
  if( v==0 ){
    goto exit_rename_table;
  }
  sqlite3MayAbort(pParse);

  /* figure out how many UTF-8 characters are in zName */
  zTabName = pTab->zName;
  nTabName = sqlite3Utf8CharLen(zTabName, -1);

  /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
  ** the schema to use the new table name.  */
  sqlite3NestedParse(pParse,
      "UPDATE \"%w\"." DFLT_SCHEMA_TABLE " SET "
      "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) "
      "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)"
      "AND   name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
      , zDb, zDb, zTabName, zName, (iDb==1), zTabName
  );

  /* Update the tbl_name and name columns of the sqlite_schema table
  ** as required.  */
  sqlite3NestedParse(pParse,
      "UPDATE %Q." DFLT_SCHEMA_TABLE " SET "
          "tbl_name = %Q, "
          "name = CASE "
            "WHEN type='table' THEN %Q "
            "WHEN name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' "
            "     AND type='index' THEN "
             "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
            "ELSE name END "
      "WHERE tbl_name=%Q COLLATE nocase AND "
          "(type='table' OR type='index' OR type='trigger');",
      zDb,
      zName, zName, zName,
      nTabName, zTabName
  );

#ifndef SQLITE_OMIT_AUTOINCREMENT
  /* If the sqlite_sequence table exists in this database, then update
  ** it with the new table name.
  */
  if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
    sqlite3NestedParse(pParse,
        "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
        zDb, zName, pTab->zName);
  }
#endif

  /* If the table being renamed is not itself part of the temp database,
  ** edit view and trigger definitions within the temp database
  ** as required.  */
  if( iDb!=1 ){
    sqlite3NestedParse(pParse,
        "UPDATE sqlite_temp_schema SET "
            "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), "
            "tbl_name = "
              "CASE WHEN tbl_name=%Q COLLATE nocase AND "
              "          sqlite_rename_test(%Q, sql, type, name, 1) "
              "THEN %Q ELSE tbl_name END "
            "WHERE type IN ('view', 'trigger')"
        , zDb, zTabName, zName, zTabName, zDb, zName);
  }

  /* If this is a virtual table, invoke the xRename() function if
  ** one is defined. The xRename() callback will modify the names
  ** of any resources used by the v-table implementation (including other
  ** SQLite tables) that are identified by the name of the virtual table.
  */
#ifndef SQLITE_OMIT_VIRTUALTABLE
  if( pVTab ){
    int i = ++pParse->nMem;
    sqlite3VdbeLoadString(v, i, zName);
    sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
  }
#endif

  renameReloadSchema(pParse, iDb);
  renameTestSchema(pParse, zDb, iDb==1);

exit_rename_table:
  sqlite3SrcListDelete(db, pSrc);
  sqlite3DbFree(db, zName);
  db->mDbFlags = savedDbFlags;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r14
movq (%rdi), %rbx
movl 0x2c(%rbx), %eax
movl %eax, 0x24(%rsp)
cmpb $0x0, 0x61(%rbx)
je 0x2bb31e
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2a63cf
movl 0x24(%rsp), %eax
movl %eax, 0x2c(%rbx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r12
movq %rdi, %r15
leaq 0x8(%r14), %rdx
xorl %esi, %esi
callq 0x2c84c3
testq %rax, %rax
je 0x2bb2fd
movq %rax, 0x8(%rsp)
movq 0x70(%rax), %rax
testq %rax, %rax
je 0x2bb362
movq (%r15), %rcx
movq 0x20(%rcx), %rcx
addq $0x18, %rcx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
incl %ebp
leaq 0x20(%rcx), %rdx
cmpq %rax, (%rcx)
movq %rdx, %rcx
jne 0x2bb352
jmp 0x2bb367
movl $0xffff8000, %ebp       # imm = 0xFFFF8000
movq 0x20(%rbx), %rax
movslq %ebp, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %r13
orl $0x2, 0x2c(%rbx)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2b1f02
testq %rax, %rax
je 0x2bb2fd
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x2811f8
testq %rax, %rax
jne 0x2bb3cb
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2a5b2b
testq %rax, %rax
jne 0x2bb3cb
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq %r12, %rdx
callq 0x2c3b87
testl %eax, %eax
je 0x2bb3fa
leaq 0x11e228(%rip), %rsi    # 0x3d95fa
movq %r15, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x27b48d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2a63cf
movq %rbx, %rdi
movq %r12, %rsi
callq 0x282904
jmp 0x2bb308
movq %r13, 0x10(%rsp)
movq %r15, %rdi
movq 0x8(%rsp), %rsi
callq 0x2e9ec3
testl %eax, %eax
jne 0x2bb3df
leaq 0x10d613(%rip), %rdx    # 0x3c8a2a
movq %r15, %rdi
movq %r12, %rsi
movq %r12, %rcx
callq 0x2c38a0
testl %eax, %eax
jne 0x2bb3df
movq 0x8(%rsp), %rax
cmpq $0x0, 0x18(%rax)
movq (%rax), %rcx
je 0x2bb447
leaq 0x11e1f6(%rip), %rsi    # 0x3d9635
movq %r15, %rdi
movq %rcx, %rdx
jmp 0x2bb3d8
movq %r15, %rdi
movl $0x1a, %esi
movq 0x10(%rsp), %rdx
xorl %r8d, %r8d
callq 0x2c37ba
testl %eax, %eax
jne 0x2bb3df
movq %r15, %rdi
movq 0x8(%rsp), %rsi
callq 0x2c8512
testl %eax, %eax
jne 0x2bb3df
movq 0x8(%rsp), %rax
cmpl $0x0, 0x54(%rax)
je 0x2bb4b5
movq 0x8(%rsp), %rax
addq $0x60, %rax
movq (%rax), %rcx
leaq 0x28(%rcx), %rax
cmpq %rbx, (%rcx)
jne 0x2bb48d
movq 0x10(%rcx), %rax
movq (%rax), %rax
movq 0x98(%rax), %rax
testq %rax, %rax
cmoveq %rax, %rcx
movq %rcx, 0x18(%rsp)
jmp 0x2bb4be
movq $0x0, 0x18(%rsp)
movq %r15, %rdi
callq 0x2bc630
movq %rax, 0x40(%rsp)
testq %rax, %rax
je 0x2bb3df
movq 0xa0(%r15), %rax
testq %rax, %rax
cmoveq %r15, %rax
movb $0x1, 0x21(%rax)
movq 0x8(%rsp), %rax
movq (%rax), %r13
movq %r13, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x2a94d7
movq %rax, 0x30(%rsp)
xorl %r10d, %r10d
cmpl $0x1, %ebp
sete %r10b
leaq 0x11e13f(%rip), %rsi    # 0x3d9650
movq %r15, %rdi
movq 0x10(%rsp), %rcx
movq %rcx, %rdx
movq %r13, %r8
movq %r12, %r9
xorl %eax, %eax
pushq %r13
movq %r10, 0x40(%rsp)
pushq %r10
callq 0x2c461b
addq $0x10, %rsp
leaq 0x11e1cb(%rip), %rsi    # 0x3d9708
movq %r15, %rdi
movq 0x10(%rsp), %rdx
movq %r12, %rcx
movq %r12, %r8
movq %r12, %r9
xorl %eax, %eax
movq %r13, 0x28(%rsp)
pushq %r13
pushq 0x38(%rsp)
callq 0x2c461b
addq $0x10, %rsp
leaq 0x11c233(%rip), %rsi    # 0x3d779e
movq %rbx, %rdi
movq 0x10(%rsp), %rdx
callq 0x2811f8
testq %rax, %rax
je 0x2bb59e
movq 0x8(%rsp), %rax
movq (%rax), %r8
leaq 0x11e2ad(%rip), %rsi    # 0x3d9839
movq %r15, %rdi
movq 0x10(%rsp), %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2c461b
cmpl $0x1, %ebp
je 0x2bb5cb
leaq 0x11e2c9(%rip), %rsi    # 0x3d9873
movq %r15, %rdi
movq 0x10(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq %r12, %r8
movq %rcx, %r9
xorl %eax, %eax
pushq %r12
pushq %rdx
callq 0x2c461b
addq $0x10, %rsp
cmpq $0x0, 0x18(%rsp)
je 0x2bb61b
movl 0x38(%r15), %esi
incl %esi
movl %esi, 0x8(%rsp)
movl %esi, 0x38(%r15)
movq 0x40(%rsp), %r13
movq %r13, %rdi
movq %r12, %rdx
callq 0x2bc906
movq %r13, %rdi
movl $0xa7, %esi
movl 0x8(%rsp), %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2a83e1
movq %r13, %rdi
movl %eax, %esi
movq 0x18(%rsp), %rdx
movl $0xfffffff4, %ecx       # imm = 0xFFFFFFF4
callq 0x27b778
movq %r15, %rdi
movl %ebp, %esi
callq 0x2e9f54
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x2e9fc5
jmp 0x2bb3df