// File: /workspace/github_c_makefile_star2_O1/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 0x398ad
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 0x398ae
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x238(%r12), %r9
testq %r9, %r9
je 0x3989f
movq %rcx, %r14
movq %rdx, %rbx
movq 0x68(%r9), %rax
testq %rax, %rax
je 0x398f2
movl 0x28(%rsi), %ecx
testl %ecx, %ecx
jle 0x398fa
movq 0x20(%rsi), %rdx
addq $0x18, %rdx
xorl %r15d, %r15d
cmpq %rax, (%rdx)
je 0x398fd
incq %r15
addq $0x20, %rdx
cmpq %r15, %rcx
jne 0x398dc
movl %ecx, %r15d
jmp 0x398fd
movl $0xfff0bdc0, %r15d      # imm = 0xFFF0BDC0
jmp 0x398fd
xorl %r15d, %r15d
movq 0x30(%r9), %r8
testq %r8, %r8
movq %rsi, -0x30(%rbp)
je 0x3993a
movq %r12, %rdi
movq %r9, %rsi
movl $0x4, %edx
xorl %ecx, %ecx
movq %r12, -0x38(%rbp)
movq %rbx, %r12
movq %r13, %rbx
movq %r9, %r13
callq 0x461b1
movq -0x30(%rbp), %rsi
movq %r13, %r9
movq %rbx, %r13
movq %r12, %rbx
movq -0x38(%rbp), %r12
cmpb $0x0, 0x99(%rsi)
je 0x39952
movl 0x94(%rsi), %eax
movl %eax, 0x3c(%r9)
jmp 0x39bb1
movq %r12, %rdi
movq %r9, -0x40(%rbp)
callq 0x45b57
testq %rax, %rax
je 0x3989f
movq %r13, -0x38(%rbp)
movq %rax, %r13
movq %rax, %rdi
movl $0x36, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
testq %rax, %rax
leaq 0x41dbc(%rip), %rcx     # 0x7b750
leaq 0x4335d(%rip), %rdx     # 0x7ccf8
cmoveq %rcx, %rdx
movq %rdx, -0x50(%rbp)
testq %r14, %r14
movq %r13, -0x58(%rbp)
je 0x39a8a
movl 0x1a4(%r12), %ecx
movq %r13, %rdi
movl $0x31, %esi
movl $0x1, %edx
movl %r15d, %r8d
callq 0x45b9e
movq 0x8(%r13), %rax
testq %rax, %rax
je 0x399e3
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 0x3ba12
movq %r13, %rdi
movl $0x36, %esi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
cmpl $0x0, 0x50(%r12)
movq -0x38(%rbp), %r13
jne 0x39a74
movq %r12, %rdi
movq %r14, %rsi
callq 0x46280
testq %rax, %rax
je 0x3989f
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 0x9939
movq -0x30(%rbp), %r14
movq %r14, %rdi
movq -0x40(%rbp), %rsi
callq 0x46377
movq %rax, -0x48(%rbp)
jmp 0x39acb
testq %rax, %rax
leaq 0x4138a(%rip), %rax     # 0x7ae1e
leaq 0x41389(%rip), %rdx     # 0x7ae24
cmoveq %rax, %rdx
movl (%rbx), %ecx
movq 0x250(%r12), %r8
subl %r8d, %ecx
incl %ecx
leaq 0x41374(%rip), %rsi     # 0x7ae29
movq -0x30(%rbp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x117e2
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %r13
movq 0x20(%r14), %rax
movslq %r15d, %rsi
shlq $0x5, %rsi
cmpl $0x1, %r15d
leaq 0x40438(%rip), %rdx     # 0x79f19
leaq 0x40444(%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 0x41326(%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 0x465b3
addq $0x20, %rsp
movq -0x30(%rbp), %rdi
movq %r14, %rsi
callq 0x9c3a
movq %r12, %rdi
movl %r15d, %esi
callq 0x466e1
movq -0x40(%rbp), %rdx
testb $0x8, 0x46(%rdx)
movq -0x30(%rbp), %rax
je 0x39b85
movq 0x20(%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x18(%rax,%rcx), %rcx
cmpq $0x0, 0x68(%rcx)
jne 0x39b85
addq -0x38(%rbp), %rax
movq (%rax), %rdx
leaq 0x41318(%rip), %rsi     # 0x7ae8f
movq %r12, %rdi
xorl %eax, %eax
callq 0x465b3
movq -0x40(%rbp), %rdx
movq (%rdx), %rdx
leaq 0x4132a(%rip), %rsi     # 0x7aeb9
movq -0x30(%rbp), %rdi
xorl %eax, %eax
callq 0x117e2
movq -0x58(%rbp), %rdi
movl %r15d, %esi
movq %rax, %rdx
callq 0x4679b
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %r9
cmpb $0x0, 0x99(%rsi)
je 0x3989f
movq (%r9), %rsi
movq 0x68(%r9), %rdi
addq $0x8, %rdi
testq %rsi, %rsi
je 0x39be9
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rsi, %rax
incl %edx
cmpb $0x0, (%rax)
leaq 0x1(%rax), %rax
jne 0x39bd6
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
jmp 0x39beb
xorl %edx, %edx
movq %r9, %rcx
movq %r9, %r14
callq 0x33cea
testq %rax, %rax
je 0x39c08
movq -0x30(%rbp), %rax
movb $0x1, 0x50(%rax)
jmp 0x3989f
movq $0x0, 0x238(%r12)
movq -0x30(%rbp), %rax
orb $0x2, 0x2c(%rax)
cmpq $0x0, 0x18(%r14)
jne 0x3989f
movq 0x250(%r12), %rdi
cmpq $0x0, (%r13)
cmovneq %r13, %rbx
movl (%rbx), %esi
subl %edi, %esi
callq 0x36ef7
addl $0xd, %eax
movl %eax, 0x48(%r14)
jmp 0x3989f