// File: /workspace/github_c_makefile_star2_O3/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 0x3b501
movq %rdx, %rbx
movq %rdi, %r15
movq (%rdi), %rdx
cmpb $0x0, 0x50(%rdx)
je 0x3b510
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x240(%r15), %r13
testq %r13, %r13
je 0x3b501
movq %r8, %r12
movl %ecx, %r14d
cmpb $0x0, 0x99(%rdx)
je 0x3b535
movl 0x94(%rdx), %eax
movl %eax, 0x3c(%r13)
testb $0x20, %r14b
movq %rsi, 0x30(%rsp)
movq %rdx, (%rsp)
je 0x3b59c
movzbl 0x48(%r13), %eax
testb $0x8, %al
jne 0x3b568
testb $0x4, %al
jne 0x3b587
movq (%r13), %rdx
leaq 0x378c9(%rip), %rsi     # 0x72e25
movq %r15, %rdi
xorl %eax, %eax
callq 0x11db0
jmp 0x3b598
leaq 0x37884(%rip), %rsi     # 0x72df3
movq %r15, %rdi
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x11db0
orb $0x20, %al
movb %al, 0x48(%r13)
movq %r15, %rdi
movq %r13, %rsi
callq 0x4d537
movq (%rsp), %rdx
movq 0x70(%r13), %rax
testq %rax, %rax
je 0x3b5cb
movl 0x28(%rdx), %ecx
testl %ecx, %ecx
jle 0x3b5d2
movq 0x20(%rdx), %rdx
addq $0x18, %rdx
xorl %ebp, %ebp
cmpq %rax, (%rdx)
je 0x3b5d4
incq %rbp
addq $0x20, %rdx
cmpq %rbp, %rcx
jne 0x3b5b6
movl %ecx, %ebp
jmp 0x3b5d4
movl $0xfff0bdc0, %ebp       # imm = 0xFFF0BDC0
jmp 0x3b5d4
xorl %ebp, %ebp
movq 0x30(%r13), %r8
testq %r8, %r8
je 0x3b5ef
movq %r15, %rdi
movq %r13, %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x4d83d
movswl 0x42(%r13), %eax
testl %eax, %eax
movq %r15, 0x8(%rsp)
jle 0x3b61b
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
ja 0x3b609
jmp 0x3b61d
xorl %ecx, %ecx
movzwl 0x40(%r13), %edi
shrl $0xf, %edi
addl %ecx, %edi
shll $0x2, %edi
callq 0x357b3
movw %ax, 0x46(%r13)
movq %r13, %r15
movq 0x10(%r13), %r13
testq %r13, %r13
je 0x3b64e
movq %r13, %rdi
callq 0x4d904
movq 0x28(%r13), %r13
jmp 0x3b63b
movq (%rsp), %rax
cmpb $0x0, 0x99(%rax)
movq %r15, %r13
movq 0x8(%rsp), %r15
je 0x3b696
movq (%r13), %rsi
movq 0x70(%r13), %rdi
addq $0x8, %rdi
testq %rsi, %rsi
je 0x3b7d5
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rsi, %rax
incl %edx
cmpb $0x0, (%rax)
leaq 0x1(%rax), %rax
jne 0x3b680
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
jmp 0x3b7d7
movq %r15, %rdi
callq 0x48791
testq %rax, %rax
je 0x3b501
movq %rax, %r15
movq %rax, %rdi
movl $0x38, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x487d8
cmpq $0x0, 0x18(%r13)
leaq 0x3777b(%rip), %rax     # 0x72e45
leaq 0x3777a(%rip), %rdx     # 0x72e4b
cmoveq %rax, %rdx
leaq 0x3802d(%rip), %rax     # 0x73709
leaq 0x395db(%rip), %rcx     # 0x74cbe
cmoveq %rax, %rcx
movq %rcx, 0x20(%rsp)
testq %r12, %r12
movq %r15, 0x28(%rsp)
je 0x3b838
movq 0x8(%rsp), %rax
movl 0x1b0(%rax), %ecx
movq %r15, %rdi
movl $0x33, %esi
movl $0x1, %edx
movl %ebp, %r8d
callq 0x487d8
movq 0x8(%r15), %rax
testq %rax, %rax
movq %r15, %r14
je 0x3b733
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 0x3d7be
movq %r14, %rdi
movl $0x38, %esi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x487d8
cmpl $0x0, 0x50(%r15)
jne 0x3b7c1
movq %r15, %rdi
movq %r12, %rsi
callq 0x4d95a
testq %rax, %rax
je 0x3b501
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 0x9810
movq (%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x4da3e
jmp 0x3b877
xorl %edx, %edx
movq %r13, %rcx
callq 0x35833
testq %rax, %rax
je 0x3b7f1
movq (%rsp), %rax
movb $0x1, 0x50(%rax)
jmp 0x3b501
movq $0x0, 0x240(%r15)
movq (%rsp), %rax
orb $0x2, 0x2c(%rax)
cmpq $0x0, 0x18(%r13)
movq 0x30(%rsp), %rax
jne 0x3b501
movq 0x258(%r15), %rdi
cmpq $0x0, (%rax)
cmovneq %rax, %rbx
movl (%rbx), %esi
subl %edi, %esi
callq 0x38a0a
addl $0xd, %eax
movl %eax, 0x4c(%r13)
jmp 0x3b501
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 0x3b862
addl 0x8(%rax), %ecx
leaq 0x375e7(%rip), %rsi     # 0x72e50
movq (%rsp), %r12
movq %r12, %rdi
xorl %eax, %eax
callq 0x11e78
movq %rax, 0x18(%rsp)
movq 0x20(%r12), %rax
movslq %ebp, %rsi
shlq $0x5, %rsi
cmpl $0x1, %ebp
leaq 0x364dc(%rip), %rdx     # 0x71d6e
leaq 0x364e8(%rip), %rcx     # 0x71d81
cmoveq %rdx, %rcx
movq %rsi, 0x10(%rsp)
movq (%rax,%rsi), %rdx
movq (%r13), %r9
movl 0x1ac(%r15), %r10d
movl 0x1b0(%r15), %r11d
leaq 0x375a0(%rip), %rsi     # 0x72e5f
movq %r15, %rdi
movq 0x20(%rsp), %r8
xorl %eax, %eax
pushq %r10
movq 0x20(%rsp), %r14
pushq %r14
pushq %r11
pushq %r9
callq 0x4dc72
addq $0x20, %rsp
movq %r12, %rdi
movq %r14, %rsi
callq 0x9b42
movq %r15, %rdi
movl %ebp, %esi
callq 0x4dda5
testb $0x8, 0x48(%r13)
je 0x3b92a
movq 0x20(%r12), %rax
movq 0x10(%rsp), %rcx
movq 0x18(%rax,%rcx), %rcx
cmpq $0x0, 0x68(%rcx)
jne 0x3b92a
addq 0x10(%rsp), %rax
movq (%rax), %rdx
leaq 0x37596(%rip), %rsi     # 0x72eb6
movq %r15, %rdi
xorl %eax, %eax
callq 0x4dc72
movq (%r13), %rdx
leaq 0x375ab(%rip), %rsi     # 0x72ee0
movq %r12, %rdi
xorl %eax, %eax
callq 0x11e78
movq 0x28(%rsp), %rdi
movl %ebp, %esi
movq %rax, %rdx
callq 0x4de55
cmpb $0x0, 0x99(%r12)
jne 0x3b663
jmp 0x3b501