// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0xd8, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x500a5
cmpq $0x0, -0x28(%rbp)
je 0x500b2
movq -0x38(%rbp), %rax
movzbl 0x50(%rax), %eax
cmpl $0x0, %eax
je 0x500b7
jmp 0x50513
movq -0x10(%rbp), %rax
movq 0x238(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x500d2
jmp 0x50513
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x68(%rax), %rsi
callq 0x13940
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x5010f
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x30(%rbp), %rax
movq 0x30(%rax), %r8
movl $0x4, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x5ee10
movq -0x38(%rbp), %rax
cmpb $0x0, 0x99(%rax)
je 0x5012d
movq -0x38(%rbp), %rax
movl 0x94(%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x3c(%rax)
movq -0x38(%rbp), %rax
cmpb $0x0, 0x99(%rax)
jne 0x5040e
movq -0x10(%rbp), %rdi
callq 0x5e000
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x50157
jmp 0x50513
movq -0x48(%rbp), %rdi
movl $0x36, %esi
xorl %edx, %edx
callq 0x5ecf0
movq -0x30(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x5018a
leaq 0x600d3(%rip), %rax     # 0xb024c
movq %rax, -0x50(%rbp)
leaq 0x5f796(%rip), %rax     # 0xaf91a
movq %rax, -0x58(%rbp)
jmp 0x501a0
leaq 0x61663(%rip), %rax     # 0xb17f4
movq %rax, -0x50(%rbp)
leaq 0x5f784(%rip), %rax     # 0xaf920
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x50288
movq -0x48(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x1a4(%rax), %ecx
movl -0x3c(%rbp), %r8d
movl $0x31, %esi
movl $0x1, %edx
callq 0x5e0f0
movq -0x48(%rbp), %rdi
movl $0x2, %esi
callq 0x5e540
movq -0x10(%rbp), %rax
movl $0x2, 0x54(%rax)
leaq -0x70(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x5ef20
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0x529a0
movq -0x48(%rbp), %rdi
movl $0x36, %esi
movl $0x1, %edx
callq 0x5ecf0
movq -0x10(%rbp), %rax
cmpl $0x0, 0x50(%rax)
jne 0x50286
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x5ef70
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0x50243
jmp 0x50513
movq -0x78(%rbp), %rax
movw 0x42(%rax), %cx
movq -0x30(%rbp), %rax
movw %cx, 0x42(%rax)
movq -0x78(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x78(%rbp), %rax
movw $0x0, 0x42(%rax)
movq -0x78(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x38(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x9a30
jmp 0x50288
cmpq $0x0, -0x28(%rbp)
je 0x502a2
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x5f0c0
movq %rax, -0x60(%rbp)
jmp 0x502e5
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x250(%rcx), %rcx
subq %rcx, %rax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
movq -0x58(%rbp), %rdx
movl -0x40(%rbp), %ecx
movq -0x10(%rbp), %rax
movq 0x250(%rax), %r8
leaq 0x5f64b(%rip), %rsi     # 0xaf925
movb $0x0, %al
callq 0x137f0
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x3c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rdx
movl -0x3c(%rbp), %esi
leaq 0x5e71f(%rip), %rcx     # 0xaea28
leaq 0x5e705(%rip), %rax     # 0xaea15
cmpl $0x1, %esi
cmoveq %rax, %rcx
movq -0x50(%rbp), %r8
movq -0x30(%rbp), %rax
movq (%rax), %r9
movq -0x30(%rbp), %rax
movq (%rax), %rbx
movq -0x10(%rbp), %rax
movl 0x1a4(%rax), %r11d
movq -0x60(%rbp), %r10
movq -0x10(%rbp), %rax
movl 0x1a0(%rax), %eax
leaq 0x5f5eb(%rip), %rsi     # 0xaf934
movq %rbx, (%rsp)
movl %r11d, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movl %eax, 0x18(%rsp)
movb $0x0, %al
callq 0x5f320
movq -0x38(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x9cd0
movq -0x10(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x5f4a0
movq -0x30(%rbp), %rax
movzbl 0x46(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x503cc
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x3c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq 0x18(%rax), %rax
cmpq $0x0, 0x68(%rax)
jne 0x503ca
movq -0x10(%rbp), %rdi
movq -0x80(%rbp), %rax
movq (%rax), %rdx
leaq 0x5f5c8(%rip), %rsi     # 0xaf98b
movb $0x0, %al
callq 0x5f320
jmp 0x503cc
movq -0x48(%rbp), %rax
movq %rax, -0xa8(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0xa0(%rbp)
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rdx
leaq 0x5f5c3(%rip), %rsi     # 0xaf9b5
movb $0x0, %al
callq 0x137f0
movq -0xa8(%rbp), %rdi
movl -0xa0(%rbp), %esi
movq %rax, %rdx
callq 0x5f530
movq -0x38(%rbp), %rax
cmpb $0x0, 0x99(%rax)
je 0x50513
movq -0x30(%rbp), %rax
movq 0x68(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xb8(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rdi
callq 0x92f0
movq -0xb8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
movl %eax, %edx
movq -0x30(%rbp), %rcx
callq 0x46e00
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x88(%rbp)
je 0x50491
movq -0x38(%rbp), %rax
movb $0x1, 0x50(%rax)
jmp 0x50513
movq -0x10(%rbp), %rax
movq $0x0, 0x238(%rax)
movq -0x38(%rbp), %rax
movl 0x2c(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x2c(%rax)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x50511
movq -0x10(%rbp), %rax
movq 0x250(%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x504dc
movq -0x20(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x98(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x9c(%rbp)
movq -0x98(%rbp), %rdi
movl -0x9c(%rbp), %esi
callq 0x4c140
movl %eax, %ecx
addl $0xd, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x48(%rax)
jmp 0x50513
addq $0xd8, %rsp
popq %rbx
popq %rbp
retq
nopl (%rax)