// File: /workspace/github_c_makefile_star2_O1/barabo[P]advanced-shell-history/src/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;
#ifndef SQLITE_OMIT_TRIGGER
  char *zWhere = 0;         /* Where clause to locate temp triggers */
#endif
  VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
  int savedDbFlags;         /* Saved value of db->flags */

  savedDbFlags = db->flags;  
  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].zName;
  db->flags |= SQLITE_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) ){
    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!=isSystemTable(pParse, pTab->zName) ){
    goto exit_rename_table;
  }
  if( SQLITE_OK!=sqlite3CheckObjectName(pParse, 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 and code the VerifyCookie for database iDb. 
  ** Then modify the schema cookie (since the ALTER TABLE modifies the
  ** schema). Open a statement transaction if the table is a virtual
  ** table.
  */
  v = sqlite3GetVdbe(pParse);
  if( v==0 ){
    goto exit_rename_table;
  }
  sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
  sqlite3ChangeCookie(pParse, iDb);

  /* 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;
    sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
    sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
    sqlite3MayAbort(pParse);
  }
#endif

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

#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
  if( db->flags&SQLITE_ForeignKeys ){
    /* If foreign-key support is enabled, rewrite the CREATE TABLE 
    ** statements corresponding to all child tables of foreign key constraints
    ** for which the renamed table is the parent table.  */
    if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
      sqlite3NestedParse(pParse, 
          "UPDATE \"%w\".%s SET "
              "sql = sqlite_rename_parent(sql, %Q, %Q) "
              "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere);
      sqlite3DbFree(db, zWhere);
    }
  }
#endif

  /* Modify the sqlite_master table to use the new table name. */
  sqlite3NestedParse(pParse,
      "UPDATE %Q.%s SET "
#ifdef SQLITE_OMIT_TRIGGER
          "sql = sqlite_rename_table(sql, %Q), "
#else
          "sql = CASE "
            "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
            "ELSE sqlite_rename_table(sql, %Q) END, "
#endif
          "tbl_name = %Q, "
          "name = CASE "
            "WHEN type='table' THEN %Q "
            "WHEN name LIKE 'sqlite_autoindex%%' 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, SCHEMA_TABLE(iDb), zName, zName, zName, 
#ifndef SQLITE_OMIT_TRIGGER
      zName,
#endif
      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

#ifndef SQLITE_OMIT_TRIGGER
  /* If there are TEMP triggers on this table, modify the sqlite_temp_master
  ** table. Don't do this if the table being ALTERed is itself located in
  ** the temp database.
  */
  if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
    sqlite3NestedParse(pParse, 
        "UPDATE sqlite_temp_master SET "
            "sql = sqlite_rename_trigger(sql, %Q), "
            "tbl_name = %Q "
            "WHERE %s;", zName, zName, zWhere);
    sqlite3DbFree(db, zWhere);
  }
#endif

#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
  if( db->flags&SQLITE_ForeignKeys ){
    FKey *p;
    for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
      Table *pFrom = p->pFrom;
      if( pFrom!=pTab ){
        reloadTableSchema(pParse, p->pFrom, pFrom->zName);
      }
    }
  }
#endif

  /* Drop and reload the internal table schema. */
  reloadTableSchema(pParse, pTab, zName);

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq (%rdi), %rbx
movl 0x2c(%rbx), %r13d
cmpb $0x0, 0x50(%rbx)
je 0x44e5f
xorl %r12d, %r12d
jmp 0x44f3c
movq %rdx, %r15
movq %rdi, %r14
movq %rsi, -0x48(%rbp)
leaq 0x8(%rsi), %rdx
xorl %r12d, %r12d
xorl %esi, %esi
callq 0x4a2cc
testq %rax, %rax
je 0x44f38
movq 0x68(%rax), %rsi
testq %rsi, %rsi
movq %rax, -0x40(%rbp)
movq %r14, -0x38(%rbp)
movl %r13d, -0x2c(%rbp)
je 0x44ec0
movq (%r14), %rdx
movl 0x28(%rdx), %ecx
testl %ecx, %ecx
jle 0x44ec8
movq 0x20(%rdx), %rdx
addq $0x18, %rdx
xorl %r13d, %r13d
cmpq %rsi, (%rdx)
je 0x44ecb
incq %r13
addq $0x20, %rdx
cmpq %r13, %rcx
jne 0x44eaa
movl %ecx, %r13d
jmp 0x44ecb
movl $0xfff0bdc0, %r13d      # imm = 0xFFF0BDC0
jmp 0x44ecb
xorl %r13d, %r13d
movq 0x20(%rbx), %rax
movslq %r13d, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %r14
orl $0x200000, 0x2c(%rbx)   # imm = 0x200000
movq %rbx, %rdi
movq %r15, %rsi
callq 0x45df3
testq %rax, %rax
je 0x44f31
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x33aab
testq %rax, %rax
jne 0x44f1a
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x3375e
testq %rax, %rax
je 0x44f62
leaq 0x37957(%rip), %rsi     # 0x7c878
movq -0x38(%rbp), %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x11724
jmp 0x44f34
xorl %r12d, %r12d
movl -0x2c(%rbp), %r13d
movq -0x48(%rbp), %rsi
movq %rbx, %rdi
callq 0x341f5
movq %rbx, %rdi
movq %r12, %rsi
callq 0x9c3a
movl %r13d, 0x2c(%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x40(%rbp), %rax
movq (%rax), %rsi
movq -0x38(%rbp), %r15
movq %r15, %rdi
callq 0x630d4
testl %eax, %eax
jne 0x44f34
movq %r15, %rdi
movq %r12, %rsi
callq 0x45f34
testl %eax, %eax
jne 0x44f34
movq -0x40(%rbp), %rax
cmpq $0x0, 0x18(%rax)
movq (%rax), %rcx
je 0x44fa6
leaq 0x37916(%rip), %rsi     # 0x7c8b3
movq -0x38(%rbp), %rdi
movq %rcx, %rdx
jmp 0x44f28
movq -0x38(%rbp), %r15
movq %r15, %rdi
movl $0x1a, %esi
movq %r14, %rdx
xorl %r8d, %r8d
callq 0x45d75
testl %eax, %eax
jne 0x44f34
movq %r14, -0x58(%rbp)
movq %r15, %rdi
movq -0x40(%rbp), %r14
movq %r14, %rsi
callq 0x4a326
testl %eax, %eax
jne 0x44f34
testb $0x10, 0x46(%r14)
jne 0x44fec
xorl %r15d, %r15d
jmp 0x45015
movq 0x58(%r14), %r15
testq %r15, %r15
je 0x45000
cmpq %rbx, (%r15)
je 0x45000
movq 0x28(%r15), %r15
jmp 0x44ff0
movq 0x10(%r15), %rax
movq (%rax), %rax
movq 0x98(%rax), %rax
testq %rax, %rax
cmoveq %rax, %r15
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0x45b57
movq %rax, -0x50(%rbp)
testq %rax, %rax
je 0x44f34
xorl %esi, %esi
testq %r15, %r15
setne %sil
movq %r14, %rdi
movl %r13d, %edx
callq 0x460a9
movq %r14, %rdi
movl %r13d, %esi
callq 0x466e1
testq %r15, %r15
movq %r15, -0x60(%rbp)
je 0x450cb
movq -0x38(%rbp), %rax
movl 0x58(%rax), %r14d
incl %r14d
movl %r14d, 0x58(%rax)
movq -0x50(%rbp), %r15
movq %r15, %rdi
movl $0x5e, %esi
xorl %edx, %edx
movl %r14d, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movq %r15, %rdi
movl %eax, %esi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x11975
movq %r15, %rdi
movl $0x8c, %esi
movl %r14d, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movq %r15, %rdi
movl %eax, %esi
movq -0x60(%rbp), %rdx
movl $0xfffffff6, %ecx       # imm = 0xFFFFFFF6
callq 0x11975
movq -0x38(%rbp), %rcx
movq 0x1d8(%rcx), %rax
testq %rax, %rax
cmoveq %rcx, %rax
movb $0x1, 0x24(%rax)
movq -0x40(%rbp), %rax
movq (%rax), %r14
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x36ef7
movq %rax, -0x50(%rbp)
testb $0x8, 0x2e(%rbx)
je 0x45145
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x631a0
testq %rax, %rax
je 0x45145
movq %rax, %r15
cmpl $0x1, %r13d
leaq 0x34e10(%rip), %rax     # 0x79f19
leaq 0x34e1c(%rip), %rcx     # 0x79f2c
cmoveq %rax, %rcx
subq $0x8, %rsp
leaq 0x377af(%rip), %rsi     # 0x7c8ce
movq -0x38(%rbp), %rdi
movq -0x58(%rbp), %rdx
movq %r14, %r8
movq %r12, %r9
xorl %eax, %eax
pushq %r15
callq 0x465b3
addq $0x10, %rsp
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9c3a
cmpl $0x1, %r13d
leaq 0x34dc9(%rip), %rax     # 0x79f19
leaq 0x34dd5(%rip), %rcx     # 0x79f2c
cmoveq %rax, %rcx
subq $0x8, %rsp
leaq 0x377ad(%rip), %rsi     # 0x7c913
movq -0x38(%rbp), %r15
movq %r15, %rdi
movq -0x58(%rbp), %rdx
movq %r12, %r8
movq %r12, %r9
xorl %eax, %eax
pushq %r14
movq %rdx, %r14
pushq -0x50(%rbp)
pushq %r12
pushq %r12
pushq %r12
callq 0x465b3
addq $0x30, %rsp
leaq 0x35c20(%rip), %rsi     # 0x7adb7
movq %rbx, %rdi
movq %r14, %rdx
callq 0x33aab
testq %rax, %rax
je 0x451c5
movq -0x40(%rbp), %rax
movq (%rax), %r8
leaq 0x378de(%rip), %rsi     # 0x7ca93
movq %r15, %rdi
movq %r14, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x465b3
movq %r15, %rdi
movq -0x40(%rbp), %rsi
callq 0x631df
testq %rax, %rax
je 0x451ff
movq %rax, %r14
leaq 0x378ed(%rip), %rsi     # 0x7cacd
movq -0x38(%rbp), %rdi
movq %r12, %rdx
movq %r12, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x465b3
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9c3a
testb $0x8, 0x2e(%rbx)
movq -0x40(%rbp), %rcx
je 0x4523e
movq %rcx, %rdi
callq 0x4fce9
movq -0x40(%rbp), %rcx
testq %rax, %rax
je 0x4523e
movq %rax, %r14
movq (%r14), %rsi
cmpq %rcx, %rsi
je 0x45235
movq (%rsi), %rdx
movq -0x38(%rbp), %rdi
callq 0x63279
movq -0x40(%rbp), %rcx
movq 0x18(%r14), %r14
testq %r14, %r14
jne 0x4521d
movq -0x38(%rbp), %rdi
movq %rcx, %rsi
movq %r12, %rdx
callq 0x63279
jmp 0x44f34