// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: sqlite3FkCheck
SQLITE_PRIVATE void sqlite3FkCheck(
  Parse *pParse,                  /* Parse context */
  Table *pTab,                    /* Row is being deleted from this table */
  int regOld,                     /* Previous row data is stored here */
  int regNew,                     /* New row data is stored here */
  int *aChange,                   /* Array indicating UPDATEd columns (or 0) */
  int bChngRowid                  /* True if rowid is UPDATEd */
){
  sqlite3 *db = pParse->db;       /* Database handle */
  FKey *pFKey;                    /* Used to iterate through FKs */
  int iDb;                        /* Index of database containing pTab */
  const char *zDb;                /* Name of database containing pTab */
  int isIgnoreErrors = pParse->disableTriggers;

  /* Exactly one of regOld and regNew should be non-zero. */
  assert( (regOld==0)!=(regNew==0) );

  /* If foreign-keys are disabled, this function is a no-op. */
  if( (db->flags&SQLITE_ForeignKeys)==0 ) return;

  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  zDb = db->aDb[iDb].zDbSName;

  /* Loop through all the foreign key constraints for which pTab is the
  ** child table (the table that the foreign key definition is part of).  */
  for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
    Table *pTo;                   /* Parent table of foreign key pFKey */
    Index *pIdx = 0;              /* Index on key columns in pTo */
    int *aiFree = 0;
    int *aiCol;
    int iCol;
    int i;
    int bIgnore = 0;

    if( aChange
     && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
     && fkChildIsModified(pTab, pFKey, aChange, bChngRowid)==0
    ){
      continue;
    }

    /* Find the parent table of this foreign key. Also find a unique index
    ** on the parent key columns in the parent table. If either of these
    ** schema items cannot be located, set an error in pParse and return
    ** early.  */
    if( pParse->disableTriggers ){
      pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
    }else{
      pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
    }
    if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
      assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
      if( !isIgnoreErrors || db->mallocFailed ) return;
      if( pTo==0 ){
        /* If isIgnoreErrors is true, then a table is being dropped. In this
        ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
        ** before actually dropping it in order to check FK constraints.
        ** If the parent table of an FK constraint on the current table is
        ** missing, behave as if it is empty. i.e. decrement the relevant
        ** FK counter for each row of the current table with non-NULL keys.
        */
        Vdbe *v = sqlite3GetVdbe(pParse);
        int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
        for(i=0; i<pFKey->nCol; i++){
          int iFromCol, iReg;
          iFromCol = pFKey->aCol[i].iFrom;
          iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol) + regOld+1;
          sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump); VdbeCoverage(v);
        }
        sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
      }
      continue;
    }
    assert( pFKey->nCol==1 || (aiFree && pIdx) );

    if( aiFree ){
      aiCol = aiFree;
    }else{
      iCol = pFKey->aCol[0].iFrom;
      aiCol = &iCol;
    }
    for(i=0; i<pFKey->nCol; i++){
      if( aiCol[i]==pTab->iPKey ){
        aiCol[i] = -1;
      }
      assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
#ifndef SQLITE_OMIT_AUTHORIZATION
      /* Request permission to read the parent key columns. If the
      ** authorization callback returns SQLITE_IGNORE, behave as if any
      ** values read from the parent table are NULL. */
      if( db->xAuth ){
        int rcauth;
        char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
        rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
        bIgnore = (rcauth==SQLITE_IGNORE);
      }
#endif
    }

    /* Take a shared-cache advisory read-lock on the parent table. Allocate
    ** a cursor to use to search the unique index on the parent key columns
    ** in the parent table.  */
    sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
    pParse->nTab++;

    if( regOld!=0 ){
      /* A row is being removed from the child table. Search for the parent.
      ** If the parent does not exist, removing the child row resolves an
      ** outstanding foreign key constraint violation. */
      fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
    }
    if( regNew!=0 && !isSetNullAction(pParse, pFKey) ){
      /* A row is being added to the child table. If a parent row cannot
      ** be found, adding the child row has violated the FK constraint.
      **
      ** If this operation is being performed as part of a trigger program
      ** that is actually a "SET NULL" action belonging to this very
      ** foreign key, then omit this scan altogether. As all child key
      ** values are guaranteed to be NULL, it is not possible for adding
      ** this row to cause an FK violation.  */
      fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1, bIgnore);
    }

    sqlite3DbFree(db, aiFree);
  }

  /* Loop through all the foreign key constraints that refer to this table.
  ** (the "child" constraints) */
  for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
    Index *pIdx = 0;              /* Foreign key index for pFKey */
    SrcList *pSrc;
    int *aiCol = 0;

    if( aChange && fkParentIsModified(pTab, pFKey, aChange, bChngRowid)==0 ){
      continue;
    }

    if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
     && !pParse->pToplevel && !pParse->isMultiWrite
    ){
      assert( regOld==0 && regNew!=0 );
      /* Inserting a single row into a parent table cannot cause (or fix)
      ** an immediate foreign key violation. So do nothing in this case.  */
      continue;
    }

    if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
      if( !isIgnoreErrors || db->mallocFailed ) return;
      continue;
    }
    assert( aiCol || pFKey->nCol==1 );

    /* Create a SrcList structure containing the child table.  We need the
    ** child table as a SrcList for sqlite3WhereBegin() */
    pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
    if( pSrc ){
      struct SrcList_item *pItem = pSrc->a;
      pItem->pTab = pFKey->pFrom;
      pItem->zName = pFKey->pFrom->zName;
      pItem->pTab->nTabRef++;
      pItem->iCursor = pParse->nTab++;

      if( regNew!=0 ){
        fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
      }
      if( regOld!=0 ){
        int eAction = pFKey->aAction[aChange!=0];
        fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
        /* If this is a deferred FK constraint, or a CASCADE or SET NULL
        ** action applies, then any foreign key violations caused by
        ** removing the parent key will be rectified by the action trigger.
        ** So do not set the "may-abort" flag in this case.
        **
        ** Note 1: If the FK is declared "ON UPDATE CASCADE", then the
        ** may-abort flag will eventually be set on this statement anyway
        ** (when this function is called as part of processing the UPDATE
        ** within the action trigger).
        **
        ** Note 2: At first glance it may seem like SQLite could simply omit
        ** all OP_FkCounter related scans when either CASCADE or SET NULL
        ** applies. The trouble starts if the CASCADE or SET NULL action
        ** trigger causes other triggers or action rules attached to the
        ** child table to fire. In these cases the fk constraint counters
        ** might be set incorrectly if any OP_FkCounter related scans are
        ** omitted.  */
        if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
          sqlite3MayAbort(pParse);
        }
      }
      pItem->zName = 0;
      sqlite3SrcListDelete(db, pSrc);
    }
    sqlite3DbFree(db, aiCol);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, 0x68(%rsp)
movq %rdx, 0x50(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, 0x10(%rsp)
movq (%rdi), %rax
movq %rax, 0x18(%rsp)
testb $0x40, 0x31(%rax)
je 0x2eae67
movl %r9d, %r13d
movq %r8, %r14
movq 0x20(%rsp), %rax
movq 0x70(%rax), %rax
testq %rax, %rax
je 0x2ea8ae
movq 0x18(%rsp), %rcx
movq 0x20(%rcx), %rcx
addq $0x18, %rcx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq 0x10(%rsp), %rsi
incl %edi
leaq 0x20(%rcx), %rdx
cmpq %rax, (%rcx)
movq %rdx, %rcx
jne 0x2ea89e
jmp 0x2ea8b8
movl $0xffff8000, %edi       # imm = 0xFFFF8000
movq 0x10(%rsp), %rsi
movb 0xd2(%rsi), %al
movb %al, 0xf(%rsp)
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rbx
testq %rbx, %rbx
movq %r14, 0x38(%rsp)
je 0x2eac7c
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rax
movslq %edi, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %r12
movq 0x50(%rsp), %rax
incl %eax
movl %eax, 0x60(%rsp)
leaq 0xf0161(%rip), %r8      # 0x3daa60
movl %r13d, 0x48(%rsp)
movl %edi, 0x4c(%rsp)
movq %r12, 0x58(%rsp)
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movq %rax, 0x28(%rsp)
testq %r14, %r14
je 0x2ea9a6
movq 0x20(%rsp), %rax
movq (%rax), %rcx
movq 0x10(%rbx), %rax
testq %rcx, %rcx
je 0x2ea95d
testq %rax, %rax
je 0x2ea962
xorl %edx, %edx
movzbl (%rcx,%rdx), %esi
movzbl (%rax,%rdx), %edi
cmpb %dil, %sil
jne 0x2ea94e
testq %rsi, %rsi
jne 0x2ea958
jmp 0x2ea9a6
movb (%rsi,%r8), %sil
cmpb (%rdi,%r8), %sil
jne 0x2ea962
incq %rdx
jmp 0x2ea93a
testq %rax, %rax
je 0x2ea9a6
movslq 0x28(%rbx), %rax
movl $0x4, %ebp
testq %rax, %rax
jle 0x2eac65
shlq $0x4, %rax
xorl %ecx, %ecx
movslq 0x40(%rbx,%rcx), %rdx
cmpl $0x0, (%r14,%rdx,4)
jns 0x2ea9a6
testl %r13d, %r13d
je 0x2ea998
movq 0x20(%rsp), %rsi
movswl 0x44(%rsi), %esi
cmpl %esi, %edx
je 0x2ea9a6
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x2ea97a
jmp 0x2eac65
movq 0x10(%rsp), %r15
cmpb $0x0, 0xd2(%r15)
movq 0x10(%rbx), %rdx
je 0x2ea9cb
movq 0x18(%rsp), %rdi
movq %rdx, %rsi
movq %r12, %rdx
callq 0x28ad59
jmp 0x2ea9d8
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rcx
callq 0x284c10
movq %rax, %r14
testq %rax, %rax
je 0x2eaa00
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
leaq 0x30(%rsp), %rcx
leaq 0x28(%rsp), %r8
callq 0x2ebf01
testl %eax, %eax
je 0x2eaac4
cmpb $0x0, 0xf(%rsp)
je 0x2eaa2d
testq %r14, %r14
setne %dl
xorl %eax, %eax
movq 0x18(%rsp), %rcx
cmpb $0x0, 0x61(%rcx)
sete %cl
setne %sil
orb %dl, %sil
je 0x2eaa3c
movb %cl, %al
leal (%rax,%rax,2), %ebp
incl %ebp
jmp 0x2eaa32
movl $0x1, %ebp
movq 0x38(%rsp), %r14
jmp 0x2eac5e
movq %r15, %rdi
callq 0x2c5ddf
movq %rax, %r14
movl 0x28(%rbx), %eax
testl %eax, %eax
movl 0x60(%rsp), %ebp
jle 0x2eaa9c
movl 0x90(%r14), %ecx
leal (%rcx,%rax), %r15d
incl %r15d
leaq 0x40(%rbx), %r12
xorl %r13d, %r13d
movswl (%r12), %esi
movq (%rbx), %rdi
callq 0x2c77b7
movswl %ax, %edx
addl %ebp, %edx
movq %r14, %rdi
movl $0x32, %esi
movl %r15d, %ecx
xorl %r8d, %r8d
callq 0x2b200c
incq %r13
movslq 0x28(%rbx), %rax
addq $0x10, %r12
cmpq %rax, %r13
jl 0x2eaa67
movzbl 0x2c(%rbx), %edx
movq %r14, %rdi
movl $0x95, %esi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x2b200c
movl $0x4, %ebp
movl 0x48(%rsp), %r13d
jmp 0x2eac54
movq 0x28(%rsp), %rcx
testq %rcx, %rcx
jne 0x2eaada
movl 0x40(%rbx), %eax
movl %eax, 0x64(%rsp)
leaq 0x64(%rsp), %rcx
cmpl $0x0, 0x28(%rbx)
movq %rcx, 0x40(%rsp)
jle 0x2eab77
movq 0x30(%rsp), %r13
leaq 0x44(%r14), %rax
movq %rax, 0x70(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %ebp, %ebp
movq 0x20(%rsp), %rax
movswl 0x44(%rax), %eax
cmpl %eax, (%rcx,%r12,2)
jne 0x2eab16
movl $0xffffffff, (%rcx,%r12,2) # imm = 0xFFFFFFFF
movq 0x18(%rsp), %rax
cmpq $0x0, 0x1c8(%rax)
je 0x2eab65
movq 0x70(%rsp), %rax
testq %r13, %r13
je 0x2eab36
movq 0x8(%r13), %rax
addq %r12, %rax
movq 0x8(%r14), %rcx
movswq (%rax), %rax
shlq $0x5, %rax
movq (%rcx,%rax), %rdx
movq (%r14), %rsi
movq 0x10(%rsp), %rdi
movl 0x4c(%rsp), %ecx
callq 0x2d03ac
xorl %ebp, %ebp
cmpl $0x2, %eax
sete %bpl
movq 0x40(%rsp), %rcx
incq %r15
movslq 0x28(%rbx), %rax
addq $0x2, %r12
cmpq %rax, %r15
jl 0x2eaaff
jmp 0x2eab79
xorl %ebp, %ebp
movl 0x38(%r14), %edx
movq (%r14), %r8
movq 0x10(%rsp), %r15
movq %r15, %rdi
movl 0x4c(%rsp), %r12d
movl %r12d, %esi
xorl %ecx, %ecx
callq 0x2c60e2
incl 0x34(%r15)
movq 0x50(%rsp), %rax
testl %eax, %eax
je 0x2eabcb
movq 0x30(%rsp), %rcx
subq $0x8, %rsp
movq %r15, %rdi
movl %r12d, %esi
movq %r14, %rdx
movq %rbx, %r8
movq 0x48(%rsp), %r9
pushq %rbp
pushq $-0x1
pushq %rax
callq 0x2ec295
addq $0x20, %rsp
movq 0x68(%rsp), %r10
testl %r10d, %r10d
movl 0x48(%rsp), %r13d
je 0x2eac3e
movq 0x10(%rsp), %rcx
movq 0xa0(%rcx), %rax
testq %rax, %rax
cmoveq %rcx, %rax
movq 0x188(%rax), %rax
testq %rax, %rax
je 0x2eac14
movq (%rax), %rax
cmpq 0x30(%rbx), %rax
jne 0x2eac08
cmpb $0x8, 0x2d(%rbx)
je 0x2eac3e
cmpq 0x38(%rbx), %rax
jne 0x2eac14
cmpb $0x8, 0x2e(%rbx)
je 0x2eac3e
movq 0x30(%rsp), %rcx
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movl %r12d, %esi
movq %r14, %rdx
movq %rbx, %r8
movq 0x48(%rsp), %r9
pushq %rbp
pushq $0x1
pushq %r10
callq 0x2ec295
addq $0x20, %rsp
movq 0x28(%rsp), %rsi
xorl %ebp, %ebp
testq %rsi, %rsi
je 0x2eac54
movq 0x18(%rsp), %rdi
callq 0x28c3e7
movq 0x38(%rsp), %r14
movq 0x58(%rsp), %r12
leaq 0xefdfb(%rip), %r8      # 0x3daa60
testb $0x3, %bpl
jne 0x2eae67
movq 0x8(%rbx), %rbx
testq %rbx, %rbx
jne 0x2ea90d
movq 0x20(%rsp), %rax
movq (%rax), %rsi
movq 0x70(%rax), %rdi
addq $0x50, %rdi
xorl %edx, %edx
callq 0x2aefdf
movq 0x10(%rax), %rbx
testq %rbx, %rbx
je 0x2eae67
xorl %eax, %eax
testq %r14, %r14
setne %al
movq %rax, 0x40(%rsp)
xorl %r15d, %r15d
leaq 0x28(%rsp), %r12
movq %r15, 0x30(%rsp)
movq %r15, 0x28(%rsp)
testq %r14, %r14
je 0x2eacdf
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movl %r13d, %ecx
callq 0x2ea55f
testl %eax, %eax
je 0x2eae45
cmpb $0x0, 0x2c(%rbx)
movq 0x10(%rsp), %rbp
jne 0x2ead09
movq 0x18(%rsp), %rax
testb $0x8, 0x32(%rax)
jne 0x2ead09
cmpq $0x0, 0xa0(%rbp)
jne 0x2ead09
cmpb $0x0, 0x20(%rbp)
je 0x2eae45
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
movq %rbx, %rdx
leaq 0x30(%rsp), %rcx
movq %r12, %r8
callq 0x2ebf01
testl %eax, %eax
je 0x2ead4b
cmpb $0x0, 0xf(%rsp)
je 0x2eae4c
movq 0x18(%rsp), %rax
cmpb $0x0, 0x61(%rax)
movl $0x1, %ebp
movl $0xd, %eax
cmovel %eax, %ebp
jmp 0x2eae51
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2bb525
testq %rax, %rax
je 0x2eae28
movq %rax, %r14
movq (%rbx), %rax
movq %rax, 0x28(%r14)
movq (%rax), %rcx
movq %rcx, 0x18(%r14)
incl 0x3c(%rax)
movq 0x10(%rsp), %rdi
movl 0x34(%rdi), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x34(%rdi)
movl %eax, 0x48(%r14)
movq 0x68(%rsp), %rax
testl %eax, %eax
je 0x2eadb2
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %r9
movq %r14, %rsi
movq 0x20(%rsp), %rdx
movq %rbx, %r8
pushq $-0x1
pushq %rax
callq 0x2ec8b9
addq $0x10, %rsp
cmpl $0x0, 0x50(%rsp)
je 0x2eae13
movq 0x40(%rsp), %rax
movb 0x2d(%rbx,%rax), %bpl
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %r9
movq 0x10(%rsp), %rdi
movq %r14, %rsi
movq 0x20(%rsp), %rdx
movq %rbx, %r8
pushq $0x1
pushq 0x58(%rsp)
callq 0x2ec8b9
addq $0x10, %rsp
cmpb $0x0, 0x2c(%rbx)
jne 0x2eae13
andb $-0x3, %bpl
cmpb $0x8, %bpl
je 0x2eae13
movq 0x10(%rsp), %rcx
movq 0xa0(%rcx), %rax
testq %rax, %rax
cmoveq %rcx, %rax
movb $0x1, 0x21(%rax)
movq $0x0, 0x18(%r14)
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x2affb4
movq 0x28(%rsp), %rsi
xorl %ebp, %ebp
testq %rsi, %rsi
movq 0x38(%rsp), %r14
je 0x2eae51
movq 0x18(%rsp), %rdi
callq 0x28c3e7
jmp 0x2eae51
movl $0xd, %ebp
jmp 0x2eae51
movl $0x1, %ebp
cmpl $0xd, %ebp
je 0x2eae5a
testl %ebp, %ebp
jne 0x2eae67
movq 0x18(%rbx), %rbx
testq %rbx, %rbx
jne 0x2eacb5
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq