// File: /workspace/github_c_makefile_star2_O0/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 %r14
pushq %rbx
subq $0xb0, %rsp
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x50(%rbp), %rax
movl 0x2c(%rax), %eax
movl %eax, -0x7c(%rbp)
movq -0x50(%rbp), %rax
cmpb $0x0, 0x50(%rax)
je 0x5d056
jmp 0x5d4a0
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rdx
addq $0x8, %rdx
xorl %esi, %esi
callq 0x64100
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x5d079
jmp 0x5d4a0
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x40(%rbp), %rax
movq 0x68(%rax), %rsi
callq 0x13940
movl %eax, -0x2c(%rbp)
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x2c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x50(%rbp), %rax
movl 0x2c(%rax), %ecx
orl $0x200000, %ecx         # imm = 0x200000
movl %ecx, 0x2c(%rax)
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x5e840
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x5d0d7
jmp 0x5d4a0
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x46b20
cmpq $0x0, %rax
jne 0x5d105
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x465f0
cmpq $0x0, %rax
je 0x5d120
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rdx
leaq 0x54260(%rip), %rsi     # 0xb1374
movb $0x0, %al
callq 0x136f0
jmp 0x5d4a0
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rax
movq (%rax), %rsi
callq 0x84980
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x5d13d
jmp 0x5d4a0
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x5eab0
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x5d157
jmp 0x5d4a0
movq -0x40(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x5d180
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rax
movq (%rax), %rdx
leaq 0x5423b(%rip), %rsi     # 0xb13af
movb $0x0, %al
callq 0x136f0
jmp 0x5d4a0
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rax
movq (%rax), %rcx
movl $0x1a, %esi
xorl %eax, %eax
movl %eax, %r8d
callq 0x5e720
cmpl $0x0, %eax
je 0x5d1a8
jmp 0x5d4a0
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x64180
cmpl $0x0, %eax
je 0x5d1bf
jmp 0x5d4a0
movq -0x40(%rbp), %rax
movzbl 0x46(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x5d1ff
movq -0x50(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x49880
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
cmpq $0x0, 0x98(%rax)
jne 0x5d1fd
movq $0x0, -0x78(%rbp)
jmp 0x5d1ff
movq -0x18(%rbp), %rdi
callq 0x5e000
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x5d218
jmp 0x5d4a0
movq -0x18(%rbp), %rdi
cmpq $0x0, -0x78(%rbp)
setne %al
andb $0x1, %al
movzbl %al, %esi
movl -0x2c(%rbp), %edx
callq 0x5ec70
movq -0x18(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x5f4a0
cmpq $0x0, -0x78(%rbp)
je 0x5d2a1
movq -0x18(%rbp), %rcx
movl 0x58(%rcx), %eax
addl $0x1, %eax
movl %eax, 0x58(%rcx)
movl %eax, -0x80(%rbp)
movq -0x68(%rbp), %rdi
movl -0x80(%rbp), %ecx
movq -0x48(%rbp), %r9
movl $0x5e, %esi
xorl %r8d, %r8d
movl %r8d, %edx
movl $0x0, (%rsp)
callq 0x5e1b0
movq -0x68(%rbp), %rdi
movl -0x80(%rbp), %edx
movq -0x78(%rbp), %r9
movl $0x8c, %esi
xorl %r8d, %r8d
movl %r8d, %ecx
movl $0xfffffff6, (%rsp)     # imm = 0xFFFFFFF6
callq 0x5e1b0
movq -0x18(%rbp), %rdi
callq 0x6a6f0
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x4c140
movl %eax, -0x54(%rbp)
movq -0x50(%rbp), %rax
movl 0x2c(%rax), %eax
andl $0x80000, %eax          # imm = 0x80000
cmpl $0x0, %eax
je 0x5d332
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x849f0
movq %rax, -0x70(%rbp)
cmpq $0x0, %rax
je 0x5d330
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rdx
movl -0x2c(%rbp), %esi
leaq 0x51731(%rip), %rcx     # 0xaea28
leaq 0x51717(%rip), %rax     # 0xaea15
cmpl $0x1, %esi
cmoveq %rax, %rcx
movq -0x60(%rbp), %r8
movq -0x48(%rbp), %r9
movq -0x70(%rbp), %rax
leaq 0x540b2(%rip), %rsi     # 0xb13ca
movq %rax, (%rsp)
movb $0x0, %al
callq 0x5f320
movq -0x50(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x9cd0
jmp 0x5d332
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rdx
movl -0x2c(%rbp), %esi
leaq 0x516e4(%rip), %rcx     # 0xaea28
leaq 0x516ca(%rip), %rax     # 0xaea15
cmpl $0x1, %esi
cmoveq %rax, %rcx
movq -0x48(%rbp), %r8
movq -0x48(%rbp), %r9
movq -0x48(%rbp), %r14
movq -0x48(%rbp), %rbx
movq -0x48(%rbp), %r11
movl -0x54(%rbp), %r10d
movq -0x60(%rbp), %rax
leaq 0x5409a(%rip), %rsi     # 0xb140f
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movl %r10d, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movb $0x0, %al
callq 0x5f320
movq -0x50(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x52510(%rip), %rsi     # 0xaf8b3
callq 0x46b20
cmpq $0x0, %rax
je 0x5d3cf
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rax
movq (%rax), %r8
leaq 0x541c7(%rip), %rsi     # 0xb158f
movb $0x0, %al
callq 0x5f320
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x84a60
movq %rax, -0x70(%rbp)
cmpq $0x0, %rax
je 0x5d411
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x70(%rbp), %r8
leaq 0x541cc(%rip), %rsi     # 0xb15c9
movb $0x0, %al
callq 0x5f320
movq -0x50(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x9cd0
movq -0x50(%rbp), %rax
movl 0x2c(%rax), %eax
andl $0x80000, %eax          # imm = 0x80000
cmpl $0x0, %eax
je 0x5d48f
movq -0x40(%rbp), %rdi
callq 0x6bb10
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x88(%rbp)
je 0x5d48d
movq -0x88(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x40(%rbp), %rax
je 0x5d477
movq -0x18(%rbp), %rdi
movq -0x88(%rbp), %rax
movq (%rax), %rsi
movq -0x90(%rbp), %rax
movq (%rax), %rdx
callq 0x84b40
jmp 0x5d479
movq -0x88(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x88(%rbp)
jmp 0x5d432
jmp 0x5d48f
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x84b40
movq -0x50(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x475c0
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x9cd0
movl -0x7c(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x2c(%rax)
addq $0xb0, %rsp
popq %rbx
popq %r14
popq %rbp
retq