// File: /workspace/github_c_makefile_star2_O1/skeeto[P]flappy/sqlite3.c
// Function: sqlite3EndTable
SQLITE_PRIVATE void sqlite3EndTable(
  Parse *pParse,          /* Parse context */
  Token *pCons,           /* The ',' token after the last column defn. */
  Token *pEnd,            /* The ')' before options in the CREATE TABLE */
  u8 tabOpts,             /* Extra table options. Usually 0. */
  Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
){
  Table *p;                 /* The new table */
  sqlite3 *db = pParse->db; /* The database connection */
  int iDb;                  /* Database in which the table lives */
  Index *pIdx;              /* An implied index of the table */

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

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

  /* 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;
  }

  /* Special processing for WITHOUT ROWID Tables */
  if( tabOpts & TF_WithoutRowid ){
    if( (p->tabFlags & TF_Autoincrement) ){
      sqlite3ErrorMsg(pParse,
          "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
      return;
    }
    if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
      sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
    }else{
      p->tabFlags |= TF_WithoutRowid;
      convertToWithoutRowidTable(pParse, p);
    }
  }

  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) */

  /* Estimate the average row size for the table and for all implied indices */
  estimateTableWidth(p);
  for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
    estimateIndexWidth(pIdx);
  }

  /* 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{
      Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
      n = (int)(pEnd2->z - pParse->sNameToken.z);
      if( pEnd2->z[0]!=';' ) n += pEnd2->n;
      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' AND type!='trigger'", 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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %rax
orq %r8, %rax
je 0x3b0f1
movq %rdx, %rbx
movq %rdi, %r15
movq (%rdi), %rdx
cmpb $0x0, 0x50(%rdx)
je 0x3b100
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x240(%r15), %r13
testq %r13, %r13
je 0x3b0f1
movq %r8, %r12
movl %ecx, %r14d
cmpb $0x0, 0x99(%rdx)
je 0x3b125
movl 0x94(%rdx), %eax
movl %eax, 0x3c(%r13)
testb $0x20, %r14b
movq %rsi, 0x30(%rsp)
movq %rdx, (%rsp)
je 0x3b18c
movzbl 0x48(%r13), %eax
testb $0x8, %al
jne 0x3b158
testb $0x4, %al
jne 0x3b177
movq (%r13), %rdx
leaq 0x36c39(%rip), %rsi     # 0x71d85
movq %r15, %rdi
xorl %eax, %eax
callq 0x11c1f
jmp 0x3b188
leaq 0x36bf4(%rip), %rsi     # 0x71d53
movq %r15, %rdi
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x11c1f
orb $0x20, %al
movb %al, 0x48(%r13)
movq %r15, %rdi
movq %r13, %rsi
callq 0x4ce51
movq (%rsp), %rdx
movq 0x70(%r13), %rax
testq %rax, %rax
je 0x3b1bb
movl 0x28(%rdx), %ecx
testl %ecx, %ecx
jle 0x3b1c2
movq 0x20(%rdx), %rdx
addq $0x18, %rdx
xorl %ebp, %ebp
cmpq %rax, (%rdx)
je 0x3b1c4
incq %rbp
addq $0x20, %rdx
cmpq %rbp, %rcx
jne 0x3b1a6
movl %ecx, %ebp
jmp 0x3b1c4
movl $0xfff0bdc0, %ebp       # imm = 0xFFF0BDC0
jmp 0x3b1c4
xorl %ebp, %ebp
movq 0x30(%r13), %r8
testq %r8, %r8
je 0x3b1df
movq %r15, %rdi
movq %r13, %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x4d1ae
movswl 0x42(%r13), %eax
testl %eax, %eax
movq %r15, 0x8(%rsp)
jle 0x3b20b
movq 0x8(%r13), %rdx
incl %eax
addq $0x2a, %rdx
xorl %ecx, %ecx
movzbl (%rdx), %esi
addl %esi, %ecx
decl %eax
addq $0x30, %rdx
cmpl $0x1, %eax
jg 0x3b1f9
jmp 0x3b20d
xorl %ecx, %ecx
movzwl 0x40(%r13), %edi
shrl $0xf, %edi
addl %ecx, %edi
shll $0x2, %edi
callq 0x3530e
movw %ax, 0x46(%r13)
movq %r13, %r15
movq 0x10(%r13), %r13
testq %r13, %r13
je 0x3b23e
movq %r13, %rdi
callq 0x4d274
movq 0x28(%r13), %r13
jmp 0x3b22b
movq (%rsp), %rax
cmpb $0x0, 0x99(%rax)
movq %r15, %r13
movq 0x8(%rsp), %r15
jne 0x3b4b6
movq %r15, %rdi
callq 0x47f6a
testq %rax, %rax
je 0x3b0f1
movq %rax, %r15
movq %rax, %rdi
movl $0x38, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x47fb1
cmpq $0x0, 0x18(%r13)
leaq 0x36b1a(%rip), %rax     # 0x71da5
leaq 0x36b19(%rip), %rdx     # 0x71dab
cmoveq %rax, %rdx
leaq 0x373cc(%rip), %rax     # 0x72669
leaq 0x3897a(%rip), %rcx     # 0x73c1e
cmoveq %rax, %rcx
movq %rcx, 0x20(%rsp)
testq %r12, %r12
movq %r15, 0x28(%rsp)
je 0x3b393
movq 0x8(%rsp), %rax
movl 0x1b0(%rax), %ecx
movq %r15, %rdi
movl $0x33, %esi
movl $0x1, %edx
movl %ebp, %r8d
callq 0x47fb1
movq 0x8(%r15), %rax
testq %rax, %rax
movq %r15, %r14
je 0x3b2f4
movslq 0x34(%r14), %rcx
leaq (%rcx,%rcx,2), %rcx
movb $0x2, -0x15(%rax,%rcx,8)
movq 0x8(%rsp), %r15
movl $0x2, 0x54(%r15)
leaq 0x38(%rsp), %rdx
movw $0x8, (%rdx)
movl $0x1, 0x4(%rdx)
movq $0x0, 0x8(%rdx)
movq %r15, %rdi
movq %r12, %rsi
callq 0x3d43a
movq %r14, %rdi
movl $0x38, %esi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x47fb1
cmpl $0x0, 0x50(%r15)
jne 0x3b382
movq %r15, %rdi
movq %r12, %rsi
callq 0x4d2ca
testq %rax, %rax
je 0x3b0f1
movzwl 0x42(%rax), %ecx
movw %cx, 0x42(%r13)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%r13)
movw $0x0, 0x42(%rax)
movq $0x0, 0x8(%rax)
movq (%rsp), %rdi
movq %rax, %rsi
callq 0x9854
movq (%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x4d3ad
jmp 0x3b3d2
movq 0x8(%rsp), %r15
leaq 0x268(%r15), %rax
testb %r14b, %r14b
cmoveq %rbx, %rax
movq (%rax), %rsi
movq 0x258(%r15), %r8
movl %esi, %ecx
subl %r8d, %ecx
cmpb $0x3b, (%rsi)
je 0x3b3bd
addl 0x8(%rax), %ecx
leaq 0x369ec(%rip), %rsi     # 0x71db0
movq (%rsp), %r12
movq %r12, %rdi
xorl %eax, %eax
callq 0x11ce7
movq %rax, 0x18(%rsp)
movq 0x20(%r12), %rax
movslq %ebp, %rsi
shlq $0x5, %rsi
cmpl $0x1, %ebp
leaq 0x358e1(%rip), %rdx     # 0x70cce
leaq 0x358ed(%rip), %rcx     # 0x70ce1
cmoveq %rdx, %rcx
movq %rsi, 0x10(%rsp)
movq (%rax,%rsi), %rdx
movq (%r13), %r9
movl 0x1ac(%r15), %r10d
movl 0x1b0(%r15), %r11d
leaq 0x369a5(%rip), %rsi     # 0x71dbf
movq %r15, %rdi
movq 0x20(%rsp), %r8
xorl %eax, %eax
pushq %r10
movq 0x20(%rsp), %r14
pushq %r14
pushq %r11
pushq %r9
callq 0x4d5e5
addq $0x20, %rsp
movq %r12, %rdi
movq %r14, %rsi
callq 0x9b58
movq %r15, %rdi
movl %ebp, %esi
callq 0x4d718
movq %r13, %r14
testb $0x8, 0x48(%r13)
je 0x3b48c
movq 0x20(%r12), %rax
movq 0x10(%rsp), %rcx
movq 0x18(%rax,%rcx), %rcx
cmpq $0x0, 0x68(%rcx)
jne 0x3b48c
addq 0x10(%rsp), %rax
movq (%rax), %rdx
leaq 0x36998(%rip), %rsi     # 0x71e16
movq %r15, %rdi
xorl %eax, %eax
callq 0x4d5e5
movq (%rsp), %r12
movq %r14, %r13
movq (%r14), %rdx
leaq 0x369a7(%rip), %rsi     # 0x71e40
movq %r12, %rdi
xorl %eax, %eax
callq 0x11ce7
movq 0x28(%rsp), %rdi
movl %ebp, %esi
movq %rax, %rdx
callq 0x4d7c8
movq (%rsp), %rax
cmpb $0x0, 0x99(%rax)
je 0x3b0f1
movq (%r13), %rsi
movq 0x70(%r13), %rdi
addq $0x8, %rdi
testq %rsi, %rsi
je 0x3b4ef
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rsi, %rax
incl %edx
cmpb $0x0, (%rax)
leaq 0x1(%rax), %rax
jne 0x3b4dc
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
jmp 0x3b4f1
xorl %edx, %edx
movq %r13, %rcx
callq 0x3538e
testq %rax, %rax
je 0x3b50b
movq (%rsp), %rax
movb $0x1, 0x50(%rax)
jmp 0x3b0f1
movq $0x0, 0x240(%r15)
movq (%rsp), %rax
orb $0x2, 0x2c(%rax)
cmpq $0x0, 0x18(%r13)
movq 0x30(%rsp), %rax
jne 0x3b0f1
movq 0x258(%r15), %rdi
cmpq $0x0, (%rax)
cmovneq %rax, %rbx
movl (%rbx), %esi
subl %edi, %esi
callq 0x385fe
addl $0xd, %eax
movl %eax, 0x4c(%r13)
jmp 0x3b0f1