// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3EndTable
SQLITE_PRIVATE void sqlite3EndTable(
  Parse *pParse,          /* Parse context */
  Token *pCons,           /* The ',' token after the last column defn. */
  Token *pEnd,            /* The final ')' token in the CREATE TABLE */
  Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
){
  Table *p;
  sqlite3 *db = pParse->db;
  int iDb;

  if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
    return;
  }
  p = pParse->pNewTable;
  if( p==0 ) return;

  assert( !db->init.busy || !pSelect );

  iDb = sqlite3SchemaToIndex(db, p->pSchema);

#ifndef SQLITE_OMIT_CHECK
  /* Resolve names in all CHECK constraint expressions.
  */
  if( p->pCheck ){
    sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
  }
#endif /* !defined(SQLITE_OMIT_CHECK) */

  /* If the db->init.busy is 1 it means we are reading the SQL off the
  ** "sqlite_master" or "sqlite_temp_master" table on the disk.
  ** So do not write to the disk again.  Extract the root page number
  ** for the table from the db->init.newTnum field.  (The page number
  ** should have been put there by the sqliteOpenCb routine.)
  */
  if( db->init.busy ){
    p->tnum = db->init.newTnum;
  }

  /* If not initializing, then create a record for the new table
  ** in the SQLITE_MASTER table of the database.
  **
  ** If this is a TEMPORARY table, write the entry into the auxiliary
  ** file instead of into the main database file.
  */
  if( !db->init.busy ){
    int n;
    Vdbe *v;
    char *zType;    /* "view" or "table" */
    char *zType2;   /* "VIEW" or "TABLE" */
    char *zStmt;    /* Text of the CREATE TABLE or CREATE VIEW statement */

    v = sqlite3GetVdbe(pParse);
    if( NEVER(v==0) ) return;

    sqlite3VdbeAddOp1(v, OP_Close, 0);

    /* 
    ** Initialize zType for the new view or table.
    */
    if( p->pSelect==0 ){
      /* A regular table */
      zType = "table";
      zType2 = "TABLE";
#ifndef SQLITE_OMIT_VIEW
    }else{
      /* A view */
      zType = "view";
      zType2 = "VIEW";
#endif
    }

    /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
    ** statement to populate the new table. The root-page number for the
    ** new table is in register pParse->regRoot.
    **
    ** Once the SELECT has been coded by sqlite3Select(), it is in a
    ** suitable state to query for the column names and types to be used
    ** by the new table.
    **
    ** A shared-cache write-lock is not required to write to the new table,
    ** as a schema-lock must have already been obtained to create it. Since
    ** a schema-lock excludes all other database users, the write-lock would
    ** be redundant.
    */
    if( pSelect ){
      SelectDest dest;
      Table *pSelTab;

      assert(pParse->nTab==1);
      sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
      sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
      pParse->nTab = 2;
      sqlite3SelectDestInit(&dest, SRT_Table, 1);
      sqlite3Select(pParse, pSelect, &dest);
      sqlite3VdbeAddOp1(v, OP_Close, 1);
      if( pParse->nErr==0 ){
        pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
        if( pSelTab==0 ) return;
        assert( p->aCol==0 );
        p->nCol = pSelTab->nCol;
        p->aCol = pSelTab->aCol;
        pSelTab->nCol = 0;
        pSelTab->aCol = 0;
        sqlite3DeleteTable(db, pSelTab);
      }
    }

    /* Compute the complete text of the CREATE statement */
    if( pSelect ){
      zStmt = createTableStmt(db, p);
    }else{
      n = (int)(pEnd->z - pParse->sNameToken.z) + 1;
      zStmt = sqlite3MPrintf(db, 
          "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
      );
    }

    /* A slot for the record has already been allocated in the 
    ** SQLITE_MASTER table.  We just need to update that slot with all
    ** the information we've collected.
    */
    sqlite3NestedParse(pParse,
      "UPDATE %Q.%s "
         "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
       "WHERE rowid=#%d",
      db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
      zType,
      p->zName,
      p->zName,
      pParse->regRoot,
      zStmt,
      pParse->regRowid
    );
    sqlite3DbFree(db, zStmt);
    sqlite3ChangeCookie(pParse, iDb);

#ifndef SQLITE_OMIT_AUTOINCREMENT
    /* Check to see if we need to create an sqlite_sequence table for
    ** keeping track of autoincrement keys.
    */
    if( p->tabFlags & TF_Autoincrement ){
      Db *pDb = &db->aDb[iDb];
      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
      if( pDb->pSchema->pSeqTab==0 ){
        sqlite3NestedParse(pParse,
          "CREATE TABLE %Q.sqlite_sequence(name,seq)",
          pDb->zName
        );
      }
    }
#endif

    /* Reparse everything to update our internal data structures */
    sqlite3VdbeAddParseSchemaOp(v, iDb,
               sqlite3MPrintf(db, "tbl_name='%q'", p->zName));
  }


  /* Add the table to the in-memory representation of the database.
  */
  if( db->init.busy ){
    Table *pOld;
    Schema *pSchema = p->pSchema;
    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
    pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName,
                             sqlite3Strlen30(p->zName),p);
    if( pOld ){
      assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
      db->mallocFailed = 1;
      return;
    }
    pParse->pNewTable = 0;
    db->flags |= SQLITE_InternChanges;

#ifndef SQLITE_OMIT_ALTERTABLE
    if( !p->pSelect ){
      const char *zName = (const char *)pParse->sNameToken.z;
      int nName;
      assert( !pSelect && pCons && pEnd );
      if( pCons->z==0 ){
        pCons = pEnd;
      }
      nName = (int)((const char *)pCons->z - zName);
      p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
    }
#endif
  }
}

// Assembly:
movq %rdx, %rax
orq %rcx, %rax
je 0x36477
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
cmpb $0x0, 0x50(%rdi)
je 0x36478
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x238(%rbx), %r13
testq %r13, %r13
je 0x36469
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r14
movq 0x68(%r13), %rsi
movq %rdi, -0x38(%rbp)
callq 0x1064e
movl %eax, -0x2c(%rbp)
movq 0x30(%r13), %r8
testq %r8, %r8
je 0x364b6
pushq $0x4
popq %rdx
movq %rbx, %rdi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x424ff
movq -0x38(%rbp), %rax
cmpb $0x0, 0x99(%rax)
je 0x3650e
movl 0x94(%rax), %eax
movl %eax, 0x3c(%r13)
movq %r14, -0x50(%rbp)
movq %rbx, %r15
movq (%r13), %rbx
movq 0x68(%r13), %r14
addq $0x8, %r14
movq %rbx, %rdi
callq 0x8f8e
movq %r14, %rdi
movq %rbx, %rsi
movl %eax, %edx
movq %r13, %rcx
callq 0x2f1a7
testq %rax, %rax
je 0x3664b
movq -0x38(%rbp), %rax
movb $0x1, 0x50(%rax)
jmp 0x36469
movq %rbx, %rdi
callq 0x41ef4
testq %rax, %rax
je 0x36469
pushq $0x36
popq %rsi
movq %rax, %rdi
movq %rax, -0x48(%rbp)
xorl %edx, %edx
callq 0x42436
movq 0x18(%r13), %rax
testq %rax, %rax
leaq 0x37232(%rip), %rcx     # 0x6d770
leaq 0x387d3(%rip), %rdx     # 0x6ed18
cmoveq %rcx, %rdx
movq %rdx, -0x90(%rbp)
testq %r15, %r15
movq %rbx, -0x40(%rbp)
je 0x367f2
movl 0x1a4(%rbx), %ecx
pushq $0x31
popq %rsi
pushq $0x1
popq %rdx
movq -0x48(%rbp), %rdi
movl -0x2c(%rbp), %r8d
callq 0x41f4c
movq -0x48(%rbp), %rdx
movq 0x8(%rdx), %rax
testq %rax, %rax
je 0x36590
movslq 0x34(%rdx), %rcx
imulq $0x18, %rcx, %rcx
movb $0x2, -0x15(%rax,%rcx)
movl $0x2, 0x54(%rbx)
leaq -0x80(%rbp), %rdx
movw $0x8, (%rdx)
movl $0x1, 0x4(%rdx)
andq $0x0, 0x8(%rdx)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x380ad
pushq $0x36
popq %rsi
pushq $0x1
popq %rdx
movq -0x48(%rbp), %rdi
callq 0x42436
cmpl $0x0, 0x50(%rbx)
jne 0x36607
movq %rbx, %rdi
movq %r15, %rsi
callq 0x425c1
testq %rax, %rax
je 0x36469
movzwl 0x42(%rax), %ecx
movw %cx, 0x42(%r13)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%r13)
andw $0x0, 0x42(%rax)
andq $0x0, 0x8(%rax)
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x93dc
movq %r14, -0x50(%rbp)
movq %r12, -0x70(%rbp)
movq 0x8(%r13), %r14
movq %r13, -0x60(%rbp)
movswl 0x42(%r13), %ebx
xorl %r15d, %r15d
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
imulq $0x30, %rax, %r13
xorl %r12d, %r12d
cmpl %r15d, %r13d
je 0x36693
movq (%r14,%r15), %rdi
callq 0x46f0e
addl %eax, %r12d
addl $0x5, %r12d
addq $0x30, %r15
jmp 0x36630
movq %r15, %rax
andq $0x0, 0x238(%r15)
movq -0x38(%rbp), %rcx
orb $0x2, 0x2c(%rcx)
cmpq $0x0, 0x18(%r13)
movq -0x50(%rbp), %r15
jne 0x36469
movq 0x250(%rax), %rdi
cmpq $0x0, (%r15)
cmovneq %r15, %r12
movl (%r12), %esi
subl %edi, %esi
callq 0x31e38
addl $0xd, %eax
movl %eax, 0x48(%r13)
jmp 0x36469
movq -0x60(%rbp), %r13
movq (%r13), %rdi
callq 0x46f0e
movl %eax, %r14d
addl %r12d, %r14d
cmpl $0x32, %r14d
leaq 0x36cbc(%rip), %rax     # 0x6d36d
leaq 0x36cb7(%rip), %rcx     # 0x6d36f
cmovlq %rax, %rcx
movq %rcx, -0x98(%rbp)
leaq 0x36cab(%rip), %rax     # 0x6d375
leaq 0x36ca3(%rip), %r12     # 0x6d374
cmovlq %rax, %r12
imull $0x6, %ebx, %eax
leal (%rax,%r14), %ebx
addl $0x23, %ebx
movl %ebx, %edi
callq 0x9c40
movq %rax, %r15
testq %rax, %rax
je 0x36865
movq %r12, -0x88(%rbp)
cmpl $0x32, %r14d
leaq 0x39c41(%rip), %rax     # 0x70345
movq %r13, %r12
leaq 0x36c62(%rip), %r13     # 0x6d370
cmovlq %rax, %r13
leaq 0x36c5e(%rip), %rdx     # 0x6d377
movl %ebx, -0x64(%rbp)
movl %ebx, %edi
movq %r15, %rsi
xorl %eax, %eax
callq 0xaf26
movq %r15, %rdi
callq 0x8f8e
leaq -0x80(%rbp), %rbx
movl %eax, (%rbx)
movq (%r12), %rdx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x46f30
movslq (%rbx), %rax
leal 0x1(%rax), %r14d
movb $0x28, (%r15,%rax)
movq 0x8(%r12), %r12
xorl %ecx, %ecx
movq -0x60(%rbp), %rax
movswl 0x42(%rax), %eax
movl -0x64(%rbp), %edi
subl %r14d, %edi
movslq %r14d, %rbx
addq %r15, %rbx
cmpl %eax, %ecx
jge 0x36837
movq %rbx, %rsi
movq %r13, %rdx
xorl %eax, %eax
movl %ecx, -0x58(%rbp)
callq 0xaf26
movq %rbx, %rdi
callq 0x8f8e
addl %r14d, %eax
movl %eax, -0x80(%rbp)
movq (%r12), %rdx
movq %r15, %rdi
leaq -0x80(%rbp), %rsi
callq 0x46f30
movsbq 0x29(%r12), %rax
leaq 0x4fce1(%rip), %rcx     # 0x86490
movq -0x308(%rcx,%rax,8), %rbx
movq %rbx, %rdi
callq 0x8f8e
movl %eax, %r14d
movslq -0x80(%rbp), %r13
movq %r13, %rdi
addq %r15, %rdi
movl %eax, %edx
movq %rbx, %rsi
callq 0x82c0
movl -0x58(%rbp), %ecx
addl %r13d, %r14d
movl %r14d, -0x80(%rbp)
incl %ecx
addq $0x30, %r12
movq -0x98(%rbp), %r13
jmp 0x36758
testq %rax, %rax
leaq 0x36642(%rip), %rax     # 0x6ce3e
leaq 0x36641(%rip), %rdx     # 0x6ce44
cmoveq %rax, %rdx
movl (%r12), %ecx
movq 0x250(%rbx), %r8
subl %r8d, %ecx
incl %ecx
leaq 0x3662b(%rip), %rsi     # 0x6ce49
movq -0x38(%rbp), %r15
movq %r15, %rdi
xorl %eax, %eax
callq 0x10594
movq %r15, %rcx
movq %rax, %r15
movq %rbx, %rdi
jmp 0x36879
leaq 0x34ebb(%rip), %rdx     # 0x6b6f9
movq %rbx, %rsi
movq -0x88(%rbp), %rcx
xorl %eax, %eax
callq 0xaf26
movq -0x70(%rbp), %r12
movq -0x60(%rbp), %r13
movq -0x50(%rbp), %r14
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rcx
jmp 0x36879
movq -0x38(%rbp), %rcx
movb $0x1, 0x50(%rcx)
movq -0x70(%rbp), %r12
movq -0x50(%rbp), %r14
movq -0x40(%rbp), %rdi
movq 0x20(%rcx), %rax
movl -0x2c(%rbp), %edx
movslq %edx, %rsi
shlq $0x5, %rsi
cmpl $0x1, %edx
movq %rcx, %rbx
leaq 0x356a5(%rip), %rdx     # 0x6bf39
leaq 0x356b1(%rip), %rcx     # 0x6bf4c
cmoveq %rdx, %rcx
movq %rsi, -0x58(%rbp)
movq (%rax,%rsi), %rdx
movq (%r13), %r9
movl 0x1a0(%rdi), %r10d
movl 0x1a4(%rdi), %r11d
leaq 0x36598(%rip), %rsi     # 0x6ce58
movq -0x90(%rbp), %r8
xorl %eax, %eax
pushq %r10
pushq %r15
pushq %r11
pushq %r9
callq 0x4268c
addq $0x20, %rsp
movq %rbx, %rdi
movq %r15, %rsi
callq 0x968b
movq -0x40(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x427b7
testb $0x8, 0x46(%r13)
je 0x36925
movq 0x20(%rbx), %rax
movq -0x58(%rbp), %rcx
movq 0x18(%rax,%rcx), %rcx
cmpq $0x0, 0x68(%rcx)
jne 0x36925
addq -0x58(%rbp), %rax
movq (%rax), %rdx
leaq 0x36595(%rip), %rsi     # 0x6ceaf
movq -0x40(%rbp), %rdi
xorl %eax, %eax
callq 0x4268c
movq (%r13), %rdx
leaq 0x365a9(%rip), %rsi     # 0x6ced9
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10594
movq -0x48(%rbp), %rdi
movl -0x2c(%rbp), %esi
movq %rax, %rdx
callq 0x42823
cmpb $0x0, 0x99(%rbx)
movq -0x40(%rbp), %rbx
jne 0x364cd
jmp 0x36469