// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/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 */
){
  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].zName;

  /* 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 isIgnore = 0;

    /* 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 iReg = pFKey->aCol[i].iFrom + regOld + 1;
          sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump);
        }
        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;
      }
#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);
        isIgnore = (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,isIgnore);
    }
    if( regNew!=0 ){
      /* 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. */ 
      fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1,isIgnore);
    }

    sqlite3DbFree(db, aiFree);
  }

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

    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 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 a single table (the table 
    ** the foreign key that refers to this table is attached to). This
    ** is required for the sqlite3WhereXXX() interface.  */
    pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
    if( pSrc ){
      struct SrcList_item *pItem = pSrc->a;
      pItem->pTab = pFKey->pFrom;
      pItem->zName = pFKey->pFrom->zName;
      pItem->pTab->nRef++;
      pItem->iCursor = pParse->nTab++;
  
      if( regNew!=0 ){
        fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
      }
      if( regOld!=0 ){
        /* If there is a RESTRICT action configured for the current operation
        ** on the parent table of this FK, then throw an exception 
        ** immediately if the FK constraint is violated, even if this is a
        ** deferred trigger. That's what RESTRICT means. To defer checking
        ** the constraint, the FK should specify NO ACTION (represented
        ** using OE_None). NO ACTION is the default.  */
        fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
      }
      pItem->zName = 0;
      sqlite3SrcListDelete(db, pSrc);
    }
    sqlite3DbFree(db, aiCol);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, -0x60(%rbp)
movq %rsi, -0x58(%rbp)
movq %rdi, %r13
movq (%rdi), %rdi
testb $0x8, 0x2e(%rdi)
jne 0x538de
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ecx, %r12d
movb 0x1f6(%r13), %al
movb %al, -0x29(%rbp)
movq -0x58(%rbp), %rbx
movq 0x68(%rbx), %rsi
movq %rdi, -0x38(%rbp)
callq 0x1064e
movq -0x38(%rbp), %rdi
movq 0x20(%rdi), %rdx
movl %eax, -0x30(%rbp)
movslq %eax, %rcx
shlq $0x5, %rcx
movq (%rdx,%rcx), %r15
leaq 0x20(%rbx), %r14
movq -0x60(%rbp), %rax
incl %eax
movl %eax, -0x6c(%rbp)
movq %r12, -0x80(%rbp)
movq %r13, -0x50(%rbp)
movq %r15, -0x78(%rbp)
movq (%r14), %r14
testq %r14, %r14
je 0x53b4d
andq $0x0, -0x48(%rbp)
andq $0x0, -0x40(%rbp)
cmpb $0x0, 0x1f6(%r13)
movq 0x10(%r14), %rdx
je 0x5395c
movq %rdx, %rsi
movq %r15, %rdx
callq 0x2eff6
jmp 0x53969
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %rcx
callq 0x10449
movq %rax, %rbx
testq %rax, %rax
je 0x5398f
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
leaq -0x48(%rbp), %rcx
leaq -0x40(%rbp), %r8
callq 0x548cc
testl %eax, %eax
je 0x53a1d
cmpb $0x0, -0x29(%rbp)
movq -0x38(%rbp), %rdi
je 0x538cf
cmpb $0x0, 0x50(%rdi)
jne 0x538cf
testq %rbx, %rbx
jne 0x53b44
movq %r13, %rdi
callq 0x41ef4
movq %rax, %rbx
movl 0x34(%rax), %ecx
movl 0x28(%r14), %eax
leal (%rcx,%rax), %r15d
incl %r15d
leaq 0x40(%r14), %r12
xorl %r13d, %r13d
cltq
cmpq %rax, %r13
jge 0x539f9
movl (%r12), %edx
addl -0x6c(%rbp), %edx
movq %rbx, %rdi
pushq $0x49
popq %rsi
movl %r15d, %ecx
callq 0x41f42
incq %r13
movl 0x28(%r14), %eax
addq $0x10, %r12
jmp 0x539d0
movzbl 0x2c(%r14), %edx
movq %rbx, %rdi
pushq $0x7b
popq %rsi
pushq $-0x1
popq %rcx
callq 0x41f42
movq -0x80(%rbp), %r12
movq -0x50(%rbp), %r13
movq -0x38(%rbp), %rdi
jmp 0x53b40
movq -0x40(%rbp), %r15
testq %r15, %r15
jne 0x53a31
movl 0x40(%r14), %eax
movl %eax, -0x70(%rbp)
leaq -0x70(%rbp), %r15
xorl %r12d, %r12d
movq -0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
xorl %r13d, %r13d
movq -0x38(%rbp), %rcx
movslq 0x28(%r14), %rax
cmpq %rax, %r12
jge 0x53ab0
movq -0x58(%rbp), %rax
movswl 0x40(%rax), %eax
cmpl %eax, (%r15,%r12,4)
jne 0x53a5f
orl $-0x1, (%r15,%r12,4)
cmpq $0x0, 0x180(%rcx)
je 0x53aab
movq -0x68(%rbp), %rax
testq %rax, %rax
je 0x53a7c
movq 0x8(%rax), %rax
movl (%rax,%r12,4), %eax
jmp 0x53a80
movswl 0x40(%rbx), %eax
movq -0x50(%rbp), %rdi
movl -0x30(%rbp), %ecx
movq 0x8(%rbx), %rdx
cltq
imulq $0x30, %rax, %rax
movq (%rdx,%rax), %rdx
movq (%rbx), %rsi
callq 0x448e5
xorl %r13d, %r13d
cmpl $0x2, %eax
sete %r13b
movq -0x38(%rbp), %rcx
incq %r12
jmp 0x53a43
movl 0x3c(%rbx), %edx
movq (%rbx), %r8
movq -0x50(%rbp), %r12
movq %r12, %rdi
movl -0x30(%rbp), %esi
xorl %ecx, %ecx
callq 0x4207f
incl 0x54(%r12)
movq -0x60(%rbp), %rax
testl %eax, %eax
je 0x53af9
subq $0x8, %rsp
movq %r12, %rdi
movl -0x30(%rbp), %esi
movq %rbx, %rdx
movq -0x68(%rbp), %rcx
movq %r14, %r8
movq %r15, %r9
pushq %r13
pushq $-0x1
pushq %rax
callq 0x54bdc
addq $0x20, %rsp
movq -0x80(%rbp), %r12
testl %r12d, %r12d
je 0x53b29
subq $0x8, %rsp
movq -0x50(%rbp), %rdi
movl -0x30(%rbp), %esi
movq %rbx, %rdx
movq -0x68(%rbp), %rcx
movq %r14, %r8
movq %r15, %r9
pushq %r13
pushq $0x1
pushq %r12
callq 0x54bdc
addq $0x20, %rsp
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
callq 0x968b
movq %rbx, %rdi
movq -0x50(%rbp), %r13
movq -0x78(%rbp), %r15
addq $0x8, %r14
jmp 0x5392b
movq -0x58(%rbp), %rdi
callq 0x4a434
movq -0x38(%rbp), %r14
movq %rax, %rbx
leaq -0x48(%rbp), %r15
testq %rbx, %rbx
je 0x538cf
andq $0x0, -0x48(%rbp)
andq $0x0, -0x40(%rbp)
cmpb $0x0, 0x2c(%rbx)
jne 0x53b96
testb $0x1, 0x2f(%r14)
jne 0x53b96
cmpq $0x0, 0x1d8(%r13)
jne 0x53b96
cmpb $0x0, 0x23(%r13)
je 0x53c7d
movq %r13, %rdi
movq -0x58(%rbp), %rsi
movq %rbx, %rdx
movq %r15, %rcx
leaq -0x40(%rbp), %r8
callq 0x548cc
testl %eax, %eax
je 0x53bce
cmpb $0x0, -0x29(%rbp)
movq -0x38(%rbp), %r14
je 0x538cf
cmpb $0x0, 0x50(%r14)
je 0x53c7d
jmp 0x538cf
movq -0x38(%rbp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x3b730
testq %rax, %rax
je 0x53c6d
movq %rax, %r15
movq (%rbx), %rax
movq %rax, 0x28(%r15)
movq (%rax), %rcx
movq %rcx, 0x18(%r15)
incw 0x44(%rax)
movl 0x54(%r13), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x54(%r13)
movl %eax, 0x44(%r15)
testl %r12d, %r12d
je 0x53c32
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %r9
movq %r13, %rdi
movq %r15, %rsi
movq -0x58(%rbp), %rdx
movq %rbx, %r8
pushq $-0x1
pushq %r12
callq 0x54fbf
popq %rax
popq %rcx
movq -0x60(%rbp), %rax
testl %eax, %eax
je 0x53c59
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %r9
movq %r13, %rdi
movq %r15, %rsi
movq -0x58(%rbp), %rdx
movq %rbx, %r8
pushq $0x1
pushq %rax
callq 0x54fbf
popq %rax
popq %rcx
andq $0x0, 0x18(%r15)
movq -0x38(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x2f60e
movq -0x40(%rbp), %rsi
movq %r14, %rdi
callq 0x968b
leaq -0x48(%rbp), %r15
movq 0x18(%rbx), %rbx
jmp 0x53b61