// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3BeginTrigger
SQLITE_PRIVATE void sqlite3BeginTrigger(
  Parse *pParse,      /* The parse context of the CREATE TRIGGER statement */
  Token *pName1,      /* The name of the trigger */
  Token *pName2,      /* The name of the trigger */
  int tr_tm,          /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
  int op,             /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
  IdList *pColumns,   /* column list if this is an UPDATE OF trigger */
  SrcList *pTableName,/* The name of the table/view the trigger applies to */
  Expr *pWhen,        /* WHEN clause */
  int isTemp,         /* True if the TEMPORARY keyword is present */
  int noErr           /* Suppress errors if the trigger already exists */
){
  Trigger *pTrigger = 0;  /* The new trigger */
  Table *pTab;            /* Table that the trigger fires off of */
  char *zName = 0;        /* Name of the trigger */
  sqlite3 *db = pParse->db;  /* The database connection */
  int iDb;                /* The database to store the trigger in */
  Token *pName;           /* The unqualified db name */
  DbFixer sFix;           /* State vector for the DB fixer */
  int iTabDb;             /* Index of the database holding pTab */

  assert( pName1!=0 );   /* pName1->z might be NULL, but not pName1 itself */
  assert( pName2!=0 );
  assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
  assert( op>0 && op<0xff );
  if( isTemp ){
    /* If TEMP was specified, then the trigger name may not be qualified. */
    if( pName2->n>0 ){
      sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
      goto trigger_cleanup;
    }
    iDb = 1;
    pName = pName1;
  }else{
    /* Figure out the db that the trigger will be created in */
    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
    if( iDb<0 ){
      goto trigger_cleanup;
    }
  }
  if( !pTableName || db->mallocFailed ){
    goto trigger_cleanup;
  }

  /* A long-standing parser bug is that this syntax was allowed:
  **
  **    CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
  **                                                 ^^^^^^^^
  **
  ** To maintain backwards compatibility, ignore the database
  ** name on pTableName if we are reparsing our of SQLITE_MASTER.
  */
  if( db->init.busy && iDb!=1 ){
    sqlite3DbFree(db, pTableName->a[0].zDatabase);
    pTableName->a[0].zDatabase = 0;
  }

  /* If the trigger name was unqualified, and the table is a temp table,
  ** then set iDb to 1 to create the trigger in the temporary database.
  ** If sqlite3SrcListLookup() returns 0, indicating the table does not
  ** exist, the error is caught by the block below.
  */
  pTab = sqlite3SrcListLookup(pParse, pTableName);
  if( db->init.busy==0 && pName2->n==0 && pTab
        && pTab->pSchema==db->aDb[1].pSchema ){
    iDb = 1;
  }

  /* Ensure the table name matches database name and that the table exists */
  if( db->mallocFailed ) goto trigger_cleanup;
  assert( pTableName->nSrc==1 );
  if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) && 
      sqlite3FixSrcList(&sFix, pTableName) ){
    goto trigger_cleanup;
  }
  pTab = sqlite3SrcListLookup(pParse, pTableName);
  if( !pTab ){
    /* The table does not exist. */
    if( db->init.iDb==1 ){
      /* Ticket #3810.
      ** Normally, whenever a table is dropped, all associated triggers are
      ** dropped too.  But if a TEMP trigger is created on a non-TEMP table
      ** and the table is dropped by a different database connection, the
      ** trigger is not visible to the database connection that does the
      ** drop so the trigger cannot be dropped.  This results in an
      ** "orphaned trigger" - a trigger whose associated table is missing.
      */
      db->init.orphanTrigger = 1;
    }
    goto trigger_cleanup;
  }
  if( IsVirtual(pTab) ){
    sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
    goto trigger_cleanup;
  }

  /* Check that the trigger name is not reserved and that no trigger of the
  ** specified name exists */
  zName = sqlite3NameFromToken(db, pName);
  if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
    goto trigger_cleanup;
  }
  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
                      zName, sqlite3Strlen30(zName)) ){
    if( !noErr ){
      sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
    }else{
      assert( !db->init.busy );
      sqlite3CodeVerifySchema(pParse, iDb);
    }
    goto trigger_cleanup;
  }

  /* Do not create a trigger on a system table */
  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
    sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
    pParse->nErr++;
    goto trigger_cleanup;
  }

  /* INSTEAD of triggers are only for views and views only support INSTEAD
  ** of triggers.
  */
  if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
    sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S", 
        (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
    goto trigger_cleanup;
  }
  if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
    sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
        " trigger on table: %S", pTableName, 0);
    goto trigger_cleanup;
  }
  iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);

#ifndef SQLITE_OMIT_AUTHORIZATION
  {
    int code = SQLITE_CREATE_TRIGGER;
    const char *zDb = db->aDb[iTabDb].zName;
    const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
    if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
    if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
      goto trigger_cleanup;
    }
    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
      goto trigger_cleanup;
    }
  }
#endif

  /* INSTEAD OF triggers can only appear on views and BEFORE triggers
  ** cannot appear on views.  So we might as well translate every
  ** INSTEAD OF trigger into a BEFORE trigger.  It simplifies code
  ** elsewhere.
  */
  if (tr_tm == TK_INSTEAD){
    tr_tm = TK_BEFORE;
  }

  /* Build the Trigger object */
  pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
  if( pTrigger==0 ) goto trigger_cleanup;
  pTrigger->zName = zName;
  zName = 0;
  pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
  pTrigger->pSchema = db->aDb[iDb].pSchema;
  pTrigger->pTabSchema = pTab->pSchema;
  pTrigger->op = (u8)op;
  pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
  pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
  pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
  assert( pParse->pNewTrigger==0 );
  pParse->pNewTrigger = pTrigger;

trigger_cleanup:
  sqlite3DbFree(db, zName);
  sqlite3SrcListDelete(db, pTableName);
  sqlite3IdListDelete(db, pColumns);
  sqlite3ExprDelete(db, pWhen);
  if( !pParse->pNewTrigger ){
    sqlite3DeleteTrigger(db, pTrigger);
  }else{
    assert( pParse->pNewTrigger==pTrigger );
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, -0x30(%rbp)
movq %rdx, %r15
movq %rdi, %rbx
movl 0x28(%rbp), %edi
movl 0x20(%rbp), %r13d
movq 0x18(%rbp), %rax
movq %rax, -0x68(%rbp)
movq 0x10(%rbp), %rdx
movq (%rbx), %r14
testl %r13d, %r13d
movq %rdx, -0x40(%rbp)
je 0x4466d
cmpl $0x0, 0x8(%r15)
je 0x446a1
leaq 0x37ef0(%rip), %rsi     # 0x7c54b
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0x11842
jmp 0x44732
movl %edi, -0x48(%rbp)
movl %r8d, -0x4c(%rbp)
movl %ecx, -0x34(%rbp)
leaq -0x70(%rbp), %rcx
movq %rbx, %rdi
movq %r15, %rdx
callq 0x46237
movq -0x40(%rbp), %rdx
movl %eax, %r12d
testl %eax, %eax
sets %al
testq %rdx, %rdx
sete %cl
orb %al, %cl
je 0x446ba
jmp 0x4472f
movl %edi, -0x48(%rbp)
movl %r8d, -0x4c(%rbp)
movl %ecx, -0x34(%rbp)
movq %rsi, -0x70(%rbp)
movl $0x1, %r12d
testq %rdx, %rdx
je 0x4472f
cmpb $0x0, 0x50(%r14)
jne 0x4472f
cmpl $0x1, %r12d
je 0x446e9
cmpb $0x0, 0x99(%r14)
je 0x446e9
movq 0x10(%rdx), %rsi
movq %r14, %rdi
callq 0x9ca4
movq -0x40(%rbp), %rdx
movq $0x0, 0x10(%rdx)
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x4bc09
cmpb $0x0, 0x99(%r14)
jne 0x44728
cmpl $0x0, 0x8(%r15)
sete %cl
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x44728
movq 0x68(%rax), %rax
movq 0x20(%r14), %rcx
cmpq 0x38(%rcx), %rax
movl $0x1, %eax
cmovel %eax, %r12d
cmpb $0x0, 0x50(%r14)
je 0x4479f
xorl %r15d, %r15d
xorl %r12d, %r12d
movq -0x30(%rbp), %r13
movq %r14, %rdi
movq %r15, %rsi
callq 0x9ca4
movq %r14, %rdi
movq -0x40(%rbp), %rsi
callq 0x3493b
movq %r14, %rdi
movq %r13, %rsi
callq 0x349e3
movq %r14, %rdi
movq -0x68(%rbp), %rsi
callq 0x3479d
cmpq $0x0, 0x240(%rbx)
je 0x44783
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
movq %r12, %rsi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x991b
movq -0x70(%rbp), %r15
cmpl $0x1, %r12d
je 0x447f2
movq (%rbx), %rax
leaq -0xa8(%rbp), %rdi
movq %rbx, (%rdi)
movq 0x20(%rax), %rax
movl %r12d, %ecx
shlq $0x5, %rcx
movq (%rax,%rcx), %rdx
movq %rdx, 0x10(%rdi)
movq 0x18(%rax,%rcx), %rax
movq %rax, 0x8(%rdi)
leaq 0x387f4(%rip), %rax     # 0x7cfcd
movq %rax, 0x18(%rdi)
movq %r15, 0x20(%rdi)
movq -0x40(%rbp), %rsi
callq 0x4bc65
testl %eax, %eax
jne 0x4472f
movq %rbx, %rdi
movq -0x40(%rbp), %rsi
callq 0x4bc09
testq %rax, %rax
je 0x44898
testb $0x10, 0x46(%rax)
jne 0x448b9
movq %rax, -0x60(%rbp)
movq %r14, %rdi
movq %r15, -0x80(%rbp)
movq %r15, %rsi
callq 0x46169
testq %rax, %rax
je 0x4472f
movq %rax, %r15
movq %rbx, %rdi
movq %rax, %rsi
callq 0x462aa
testl %eax, %eax
jne 0x44732
movq 0x20(%r14), %rcx
movl %r12d, %eax
shlq $0x5, %rax
movq %rax, -0x78(%rbp)
movq %rcx, -0x58(%rbp)
movq 0x18(%rcx,%rax), %rdi
xorl %edx, %edx
cmpb $0x0, (%r15,%rdx)
leaq 0x1(%rdx), %rdx
jne 0x4485d
addq $0x38, %rdi
decl %edx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
movq %r15, %rsi
callq 0x3406c
testq %rax, %rax
je 0x448c5
cmpl $0x0, -0x48(%rbp)
movq -0x30(%rbp), %r13
je 0x44902
movq %rbx, %rdi
movl %r12d, %esi
callq 0x4632f
jmp 0x448b1
cmpb $0x1, 0x98(%r14)
movq -0x30(%rbp), %r13
jne 0x448ae
movb $0x1, 0x9a(%r14)
xorl %r15d, %r15d
xorl %r12d, %r12d
jmp 0x44739
leaq 0x37cb9(%rip), %rsi     # 0x7c579
jmp 0x4465b
movq -0x60(%rbp), %r9
movq (%r9), %rcx
xorl %edx, %edx
leaq 0x317db(%rip), %rax     # 0x760b0
leaq 0x36510(%rip), %rsi     # 0x7adec
movzbl (%rcx,%rdx), %edi
testq %rdi, %rdi
je 0x4491f
movzbl (%rdi,%rax), %edi
movzbl (%rdx,%rsi), %r8d
movb (%r8,%rax), %r8b
cmpb %r8b, %dil
jne 0x44929
incq %rdx
cmpq $0x7, %rdx
jne 0x448dc
jmp 0x44931
leaq 0x37c99(%rip), %rsi     # 0x7c5a2
xorl %r12d, %r12d
movq %rbx, %rdi
movq -0x80(%rbp), %rdx
xorl %eax, %eax
callq 0x11842
jmp 0x44739
movzbl (%rdx,%rsi), %edx
movb (%rdx,%rax), %r8b
xorl %edi, %edi
movzbl %r8b, %eax
cmpl %eax, %edi
jne 0x4494d
leaq 0x37c84(%rip), %rsi     # 0x7c5bc
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0x11842
incl 0x50(%rbx)
jmp 0x44735
movq 0x18(%r9), %rax
testq %rax, %rax
sete %dl
movl -0x34(%rbp), %edi
cmpl $0x30, %edi
sete %sil
orb %dl, %sil
jne 0x4499b
cmpl $0x22, %edi
leaq 0x37c97(%rip), %rax     # 0x7c607
leaq 0x37c97(%rip), %rdx     # 0x7c60e
cmoveq %rax, %rdx
leaq 0x37c60(%rip), %rsi     # 0x7c5e2
xorl %r12d, %r12d
movq %rbx, %rdi
movq -0x40(%rbp), %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x11842
jmp 0x44735
testq %rax, %rax
setne %al
cmpl $0x30, %edi
setne %dl
orb %al, %dl
jne 0x449ca
leaq 0x37c62(%rip), %rsi     # 0x7c614
xorl %r12d, %r12d
movq %rbx, %rdi
movq -0x40(%rbp), %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x11842
jmp 0x44735
movl %edi, -0x34(%rbp)
movq 0x68(%r9), %rax
testq %rax, %rax
je 0x449ff
movl 0x28(%r14), %edx
testl %edx, %edx
jle 0x44a07
movq -0x58(%rbp), %rsi
addq $0x18, %rsi
xorl %r12d, %r12d
cmpq %rax, (%rsi)
je 0x44a0a
incq %r12
addq $0x20, %rsi
cmpq %r12, %rdx
jne 0x449e9
movl %edx, %r12d
jmp 0x44a0a
movl $0xfff0bdc0, %r12d      # imm = 0xFFF0BDC0
jmp 0x44a0a
xorl %r12d, %r12d
movslq %r12d, %rax
shlq $0x5, %rax
movq -0x58(%rbp), %rdx
movq (%rdx,%rax), %r8
movq %r8, -0x48(%rbp)
testl %r13d, %r13d
je 0x44a2a
movq -0x58(%rbp), %rax
movq 0x20(%rax), %r8
xorl %eax, %eax
cmpl $0x1, %r12d
setne %al
testl %r13d, %r13d
leal 0x5(%rax,%rax), %eax
movl $0x5, %esi
cmovel %eax, %esi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x460eb
testl %eax, %eax
jne 0x44732
cmpl $0x1, %r12d
leaq 0x354b9(%rip), %rax     # 0x79f19
leaq 0x354c5(%rip), %rdx     # 0x79f2c
cmoveq %rax, %rdx
xorl %r12d, %r12d
movq %rbx, %rdi
movl $0x12, %esi
xorl %ecx, %ecx
movq -0x48(%rbp), %r8
callq 0x460eb
testl %eax, %eax
jne 0x44735
movq %r14, %rdi
movl $0x48, %esi
callq 0x11721
testq %rax, %rax
je 0x44732
movq %rax, %r12
movl -0x34(%rbp), %r13d
cmpl $0x30, %r13d
sete -0x48(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%rax)
movups %xmm0, 0x28(%rax)
movups %xmm0, 0x18(%rax)
movups %xmm0, 0x8(%rax)
movq %r15, (%rax)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rsi
movq %r14, %rdi
callq 0x101fc
movq %rax, 0x8(%r12)
movq 0x20(%r14), %rax
movq -0x78(%rbp), %rcx
movq 0x18(%rax,%rcx), %rax
movq %rax, 0x28(%r12)
movq -0x60(%rbp), %rax
movq 0x68(%rax), %rax
movq %rax, 0x30(%r12)
movl -0x4c(%rbp), %eax
movb %al, 0x10(%r12)
cmpl $0x22, %r13d
sete %al
orb -0x48(%rbp), %al
movb $0x2, %cl
subb %al, %cl
movb %cl, 0x11(%r12)
xorl %r15d, %r15d
movq %r14, %rdi
movq -0x68(%rbp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x4848c
movq %rax, 0x18(%r12)
movq %r14, %rdi
movq -0x30(%rbp), %r13
movq %r13, %rsi
callq 0x48be7
movq %rax, 0x20(%r12)
movq %r12, 0x240(%rbx)
jmp 0x44739