// File: /workspace/github_c_makefile_star2_O3/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 0x39f6b
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r13
movq %rdi, %r12
movq (%rdi), %rsi
cmpb $0x0, 0x50(%rsi)
je 0x39f6c
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x238(%r12), %r9
testq %r9, %r9
je 0x39f5d
movq %rcx, %r14
movq %rdx, %rbx
movq 0x68(%r9), %rax
testq %rax, %rax
je 0x39fb0
movl 0x28(%rsi), %ecx
testl %ecx, %ecx
jle 0x39fb8
movq 0x20(%rsi), %rdx
addq $0x18, %rdx
xorl %r15d, %r15d
cmpq %rax, (%rdx)
je 0x39fbb
incq %r15
addq $0x20, %rdx
cmpq %r15, %rcx
jne 0x39f9a
movl %ecx, %r15d
jmp 0x39fbb
movl $0xfff0bdc0, %r15d      # imm = 0xFFF0BDC0
jmp 0x39fbb
xorl %r15d, %r15d
movq 0x30(%r9), %r8
testq %r8, %r8
movq %rsi, -0x30(%rbp)
je 0x39ff2
movq %r12, %rdi
movq %r9, %rsi
movl $0x4, %edx
xorl %ecx, %ecx
movq %rbx, -0x38(%rbp)
movq %r13, %rbx
movq %r9, %r13
callq 0x464e9
movq -0x30(%rbp), %rsi
movq %r13, %r9
movq %rbx, %r13
movq -0x38(%rbp), %rbx
cmpb $0x0, 0x99(%rsi)
je 0x3a037
movl 0x94(%rsi), %eax
movl %eax, 0x3c(%r9)
movq (%r9), %rsi
movq 0x68(%r9), %rdi
addq $0x8, %rdi
testq %rsi, %rsi
je 0x3a172
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rsi, %rax
incl %edx
cmpb $0x0, (%rax)
leaq 0x1(%rax), %rax
jne 0x3a021
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
jmp 0x3a174
movq %r12, %rdi
movq %r9, -0x40(%rbp)
callq 0x45ece
testq %rax, %rax
je 0x39f5d
movq %r13, -0x38(%rbp)
movq %rax, %r13
movq %rax, %rdi
movl $0x36, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45f15
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
testq %rax, %rax
leaq 0x416d7(%rip), %rcx     # 0x7b750
leaq 0x42c78(%rip), %rdx     # 0x7ccf8
cmoveq %rcx, %rdx
movq %rdx, -0x50(%rbp)
testq %r14, %r14
movq %r13, -0x58(%rbp)
je 0x3a1d6
movl 0x1a4(%r12), %ecx
movq %r13, %rdi
movl $0x31, %esi
movl $0x1, %edx
movl %r15d, %r8d
callq 0x45f15
movq 0x8(%r13), %rax
testq %rax, %rax
je 0x3a0c8
movslq 0x34(%r13), %rcx
leaq (%rcx,%rcx,2), %rcx
movb $0x2, -0x15(%rax,%rcx,8)
movl $0x2, 0x54(%r12)
leaq -0x68(%rbp), %rdx
movw $0x8, (%rdx)
movl $0x1, 0x4(%rdx)
movq $0x0, 0x8(%rdx)
movq %r12, %rdi
movq %r14, %rsi
callq 0x3bffc
movq %r13, %rdi
movl $0x36, %esi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45f15
cmpl $0x0, 0x50(%r12)
movq -0x38(%rbp), %r13
jne 0x3a159
movq %r12, %rdi
movq %r14, %rsi
callq 0x465b5
testq %rax, %rax
je 0x39f5d
movzwl 0x42(%rax), %ecx
movq -0x40(%rbp), %rdx
movw %cx, 0x42(%rdx)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rdx)
movw $0x0, 0x42(%rax)
movq $0x0, 0x8(%rax)
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x9975
movq -0x30(%rbp), %r14
movq %r14, %rdi
movq -0x40(%rbp), %rsi
callq 0x466a9
movq %rax, -0x48(%rbp)
jmp 0x3a217
xorl %edx, %edx
movq %r9, %rcx
movq %r9, %r14
callq 0x34400
testq %rax, %rax
je 0x3a191
movq -0x30(%rbp), %rax
movb $0x1, 0x50(%rax)
jmp 0x39f5d
movq $0x0, 0x238(%r12)
movq -0x30(%rbp), %rax
orb $0x2, 0x2c(%rax)
cmpq $0x0, 0x18(%r14)
jne 0x39f5d
movq 0x250(%r12), %rdi
cmpq $0x0, (%r13)
cmovneq %r13, %rbx
movl (%rbx), %esi
subl %edi, %esi
callq 0x375df
addl $0xd, %eax
movl %eax, 0x48(%r14)
jmp 0x39f5d
testq %rax, %rax
leaq 0x40c3e(%rip), %rax     # 0x7ae1e
leaq 0x40c3d(%rip), %rdx     # 0x7ae24
cmoveq %rax, %rdx
movl (%rbx), %ecx
movq 0x250(%r12), %r8
subl %r8d, %ecx
incl %ecx
leaq 0x40c28(%rip), %rsi     # 0x7ae29
movq -0x30(%rbp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x11900
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %r13
movq 0x20(%r14), %rax
movslq %r15d, %rsi
shlq $0x5, %rsi
cmpl $0x1, %r15d
leaq 0x3fcec(%rip), %rdx     # 0x79f19
leaq 0x3fcf8(%rip), %rcx     # 0x79f2c
cmoveq %rdx, %rcx
movq %rsi, -0x38(%rbp)
movq (%rax,%rsi), %rdx
movq -0x40(%rbp), %rax
movq (%rax), %r9
movl 0x1a0(%r12), %r10d
movl 0x1a4(%r12), %r11d
leaq 0x40bda(%rip), %rsi     # 0x7ae38
movq %r12, %rdi
movq -0x50(%rbp), %r8
xorl %eax, %eax
pushq %r10
movq -0x48(%rbp), %r14
pushq %r14
pushq %r11
pushq %r9
callq 0x468e4
addq $0x20, %rsp
movq -0x30(%rbp), %rdi
movq %r14, %rsi
callq 0x9ca4
movq %r12, %rdi
movl %r15d, %esi
callq 0x46a12
movq -0x30(%rbp), %r14
movq -0x40(%rbp), %rdx
testb $0x8, 0x46(%rdx)
je 0x3a2d1
movq 0x20(%r14), %rax
movq -0x38(%rbp), %rcx
movq 0x18(%rax,%rcx), %rcx
cmpq $0x0, 0x68(%rcx)
jne 0x3a2d1
addq -0x38(%rbp), %rax
movq (%rax), %rdx
leaq 0x40bcc(%rip), %rsi     # 0x7ae8f
movq %r12, %rdi
xorl %eax, %eax
callq 0x468e4
movq -0x40(%rbp), %rdx
movq (%rdx), %rdx
leaq 0x40bde(%rip), %rsi     # 0x7aeb9
movq %r14, %rdi
xorl %eax, %eax
callq 0x11900
movq -0x58(%rbp), %rdi
movl %r15d, %esi
movq %rax, %rdx
callq 0x46acc
movq -0x40(%rbp), %r9
cmpb $0x0, 0x99(%r14)
jne 0x3a005
jmp 0x39f5d