// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: sqlite3AlterFinishAddColumn
SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
  Table *pNew;              /* Copy of pParse->pNewTable */
  Table *pTab;              /* Table being altered */
  int iDb;                  /* Database number */
  const char *zDb;          /* Database name */
  const char *zTab;         /* Table name */
  char *zCol;               /* Null-terminated column definition */
  Column *pCol;             /* The new column */
  Expr *pDflt;              /* Default value for the new column */
  sqlite3 *db;              /* The database connection; */
  Vdbe *v;                  /* The prepared statement under construction */
  int r1;                   /* Temporary registers */

  db = pParse->db;
  if( pParse->nErr || db->mallocFailed ) return;
  pNew = pParse->pNewTable;
  assert( pNew );

  assert( sqlite3BtreeHoldsAllMutexes(db) );
  iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
  zDb = db->aDb[iDb].zDbSName;
  zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
  pCol = &pNew->aCol[pNew->nCol-1];
  pDflt = pCol->pDflt;
  pTab = sqlite3FindTable(db, zTab, zDb);
  assert( pTab );

#ifndef SQLITE_OMIT_AUTHORIZATION
  /* Invoke the authorization callback. */
  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
    return;
  }
#endif


  /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
  ** If there is a NOT NULL constraint, then the default value for the
  ** column must not be NULL.
  */
  if( pCol->colFlags & COLFLAG_PRIMKEY ){
    sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
    return;
  }
  if( pNew->pIndex ){
    sqlite3ErrorMsg(pParse,
         "Cannot add a UNIQUE column");
    return;
  }
  if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){
    /* If the default value for the new column was specified with a
    ** literal NULL, then set pDflt to 0. This simplifies checking
    ** for an SQL NULL default below.
    */
    assert( pDflt==0 || pDflt->op==TK_SPAN );
    if( pDflt && pDflt->pLeft->op==TK_NULL ){
      pDflt = 0;
    }
    if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
      sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
          "Cannot add a REFERENCES column with non-NULL default value");
    }
    if( pCol->notNull && !pDflt ){
      sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
          "Cannot add a NOT NULL column with default value NULL");
    }


    /* Ensure the default expression is something that sqlite3ValueFromExpr()
    ** can handle (i.e. not CURRENT_TIME etc.)
    */
    if( pDflt ){
      sqlite3_value *pVal = 0;
      int rc;
      rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
      assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
      if( rc!=SQLITE_OK ){
        assert( db->mallocFailed == 1 );
        return;
      }
      if( !pVal ){
        sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
           "Cannot add a column with non-constant default");
      }
      sqlite3ValueFree(pVal);
    }
  }else if( pCol->colFlags & COLFLAG_STORED ){
    sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, "cannot add a STORED column");
  }


  /* Modify the CREATE TABLE statement. */
  zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
  if( zCol ){
    char *zEnd = &zCol[pColDef->n-1];
    u32 savedDbFlags = db->mDbFlags;
    while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
      *zEnd-- = '\0';
    }
    db->mDbFlags |= DBFLAG_PreferBuiltin;
    sqlite3NestedParse(pParse,
        "UPDATE \"%w\"." DFLT_SCHEMA_TABLE " SET "
          "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
        "WHERE type = 'table' AND name = %Q",
      zDb, pNew->addColOffset, zCol, pNew->addColOffset+1,
      zTab
    );
    sqlite3DbFree(db, zCol);
    db->mDbFlags = savedDbFlags;
  }

  /* Make sure the schema version is at least 3.  But do not upgrade
  ** from less than 3 to 4, as that will corrupt any preexisting DESC
  ** index.
  */
  v = sqlite3GetVdbe(pParse);
  if( v ){
    r1 = sqlite3GetTempReg(pParse);
    sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
    sqlite3VdbeUsesBtree(v, iDb);
    sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
    sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2);
    VdbeCoverage(v);
    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3);
    sqlite3ReleaseTempReg(pParse, r1);
  }

  /* Reload the table definition */
  renameReloadSchema(pParse, iDb);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
cmpl $0x0, 0x30(%rdi)
jne 0x2bb707
movq %rdi, %rdx
movq (%rdi), %rdi
cmpb $0x0, 0x61(%rdi)
jne 0x2bb707
movq 0x148(%rdx), %r14
movq 0x70(%r14), %rcx
movq 0x20(%rdi), %rax
testq %rcx, %rcx
movq %rsi, 0x28(%rsp)
movq %rdx, 0x18(%rsp)
je 0x2bb69b
leaq 0x18(%rax), %rdx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
incl %ebp
leaq 0x20(%rdx), %rsi
cmpq %rcx, (%rdx)
movq %rsi, %rdx
jne 0x2bb68b
jmp 0x2bb6a0
movl $0xffff8000, %ebp       # imm = 0xFFFF8000
movslq %ebp, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %r13
movq (%r14), %r12
movq 0x8(%r14), %rax
addq $0x10, %r12
movswq 0x46(%r14), %r15
shlq $0x5, %r15
movq %rax, 0x30(%rsp)
movq -0x18(%rax,%r15), %rax
movq %rax, 0x38(%rsp)
movq %r12, %rsi
movq %r13, %rdx
movq %rdi, 0x10(%rsp)
callq 0x2811f8
movq (%rax), %rcx
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movl $0x1a, %esi
movq %r13, %rdx
xorl %r8d, %r8d
callq 0x2c37ba
movq 0x10(%rsp), %rdi
testl %eax, %eax
movq 0x28(%rsp), %rcx
je 0x2bb716
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %r8
movq 0x38(%rsp), %rsi
movq %r13, 0x20(%rsp)
movq 0x30(%rsp), %rdx
addq %r15, %rdx
movzwl -0x4(%rdx), %eax
testb $0x1, %al
jne 0x2bb743
cmpq $0x0, 0x10(%r14)
je 0x2bb762
leaq 0x11e3b9(%rip), %rsi    # 0x3d9afa
jmp 0x2bb74a
leaq 0x11e390(%rip), %rsi    # 0x3d9ada
movq %r8, %rdi
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x27b48d
movq %r14, %r15
testb $0x60, %al
je 0x2bb782
testb $0x40, %al
je 0x2bb7df
leaq 0x11e4d2(%rip), %rsi    # 0x3d9c46
leaq 0x11e438(%rip), %rdx    # 0x3d9bb3
movq 0x18(%rsp), %rdi
jmp 0x2bb7c6
testq %rsi, %rsi
je 0x2bb796
movq 0x10(%rsi), %rax
xorl %ebx, %ebx
cmpb $0x77, (%rax)
cmovneq %rsi, %rbx
jmp 0x2bb798
xorl %ebx, %ebx
testb $0x40, 0x31(%rdi)
jne 0x2bb98d
cmpb $0x0, -0x8(%rdx)
je 0x2bb9c8
testq %rbx, %rbx
jne 0x2bb9c8
leaq 0x11e48a(%rip), %rsi    # 0x3d9c46
leaq 0x11e38d(%rip), %rdx    # 0x3d9b50
movq %r8, %rdi
movq 0x20(%rsp), %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x2c461b
movq 0x28(%rsp), %rcx
movq 0x10(%rsp), %rdi
movq (%rcx), %r14
testq %r14, %r14
je 0x2bb8a5
movl 0x8(%rcx), %r13d
leaq 0x1(%r13), %rsi
callq 0x282a35
movq 0x28(%rsp), %rcx
testq %rax, %rax
je 0x2bb8a5
movq %rax, %rbx
movq %r12, 0x30(%rsp)
movq %rax, %rdi
movq %rcx, %r12
movq %r14, %rsi
movq %r13, %rdx
callq 0x17660
movq 0x10(%rsp), %r14
movb $0x0, (%rbx,%r13)
movl 0x8(%r12), %ecx
movl 0x2c(%r14), %r12d
movl %r12d, %eax
decl %ecx
movq 0x20(%rsp), %rdx
movq 0x18(%rsp), %rdi
je 0x2bb86d
movl %ecx, %eax
addq %rbx, %rax
leaq 0x115bf1(%rip), %rcx    # 0x3d1440
movzbl (%rax), %esi
cmpq $0x3b, %rsi
je 0x2bb85e
testb $0x1, (%rsi,%rcx)
je 0x2bb869
movb $0x0, (%rax)
decq %rax
cmpq %rbx, %rax
ja 0x2bb84f
movl 0x2c(%r14), %eax
orl $0x2, %eax
movl %eax, 0x2c(%r14)
movl 0x50(%r15), %ecx
leal 0x1(%rcx), %r9d
movq 0x30(%rsp), %rax
movq %rax, (%rsp)
leaq 0x11e342(%rip), %rsi    # 0x3d9bce
movq %rbx, %r8
xorl %eax, %eax
callq 0x2c461b
movq %r14, %rdi
movq %rbx, %rsi
callq 0x282904
movl %r12d, 0x2c(%r14)
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x2bc630
testq %rax, %rax
je 0x2bb975
movq %rax, %r15
movb 0x1f(%rbx), %al
testb %al, %al
je 0x2bb8d7
decb %al
movb %al, 0x1f(%rbx)
movzbl %al, %eax
movl 0xd4(%rbx,%rax,4), %r14d
jmp 0x2bb8e2
movl 0x38(%rbx), %r14d
incl %r14d
movl %r14d, 0x38(%rbx)
movq %r15, %rdi
movl $0x5d, %esi
movl %ebp, %edx
movl %r14d, %ecx
movl $0x2, %r8d
callq 0x2a83e1
movq %r15, %rdi
movl %ebp, %esi
callq 0x27b744
movq %r15, %rdi
movl $0x52, %esi
movl %r14d, %edx
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
xorl %r8d, %r8d
callq 0x2a83e1
movl 0x90(%r15), %ecx
addl $0x2, %ecx
movq %r15, %rdi
movl $0x30, %esi
movl %r14d, %edx
xorl %r8d, %r8d
callq 0x2a83e1
movq %r15, %rdi
movl $0x5e, %esi
movl %ebp, %edx
movl $0x2, %ecx
movl $0x3, %r8d
callq 0x2a83e1
testl %r14d, %r14d
movq 0x18(%rsp), %rbx
je 0x2bb975
movzbl 0x1f(%rbx), %eax
cmpq $0x7, %rax
ja 0x2bb975
leal 0x1(%rax), %ecx
movb %cl, 0x1f(%rbx)
movl %r14d, 0xd4(%rbx,%rax,4)
movq %rbx, %rdi
movl %ebp, %esi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2e9f54
cmpq $0x0, 0x20(%r15)
je 0x2bb7a2
testq %rbx, %rbx
je 0x2bb7a2
leaq 0x11e29e(%rip), %rsi    # 0x3d9c46
leaq 0x11e166(%rip), %rdx    # 0x3d9b15
movq %r8, %rdi
movq 0x20(%rsp), %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x2c461b
movq 0x10(%rsp), %rdi
jmp 0x2bb9d1
testq %rbx, %rbx
je 0x2bb7df
leaq 0x40(%rsp), %r8
movq $0x0, (%r8)
movq %rbx, %rsi
movl $0x1, %edx
movl $0x41, %ecx
callq 0x2c0365
testl %eax, %eax
jne 0x2bb707
movq 0x40(%rsp), %rbx
testq %rbx, %rbx
jne 0x2bba23
leaq 0x11e23e(%rip), %rsi    # 0x3d9c46
leaq 0x11e176(%rip), %rdx    # 0x3d9b85
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x2c461b
movq %rbx, %rdi
callq 0x278453
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rcx
jmp 0x2bb7df