// File: /workspace/github_c_makefile_star2_O3/skeeto[P]flappy/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].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;

    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 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.
  ** (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 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(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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, 0x60(%rsp)
movq %rdx, 0x38(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, 0x8(%rsp)
movq (%rdi), %rdi
testb $0x8, 0x2e(%rdi)
jne 0x60732
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r9d, %r13d
movq %r8, %rbp
movq 0x18(%rsp), %rax
movq 0x70(%rax), %rax
testq %rax, %rax
je 0x60771
movl 0x28(%rdi), %ecx
testl %ecx, %ecx
movq 0x8(%rsp), %r15
jle 0x6077d
movq 0x20(%rdi), %rdx
addq $0x18, %rdx
xorl %esi, %esi
cmpq %rax, (%rdx)
je 0x6077f
incq %rsi
addq $0x20, %rdx
cmpq %rsi, %rcx
jne 0x6075c
movl %ecx, %esi
jmp 0x6077f
movl $0xfff0bdc0, %esi       # imm = 0xFFF0BDC0
movq 0x8(%rsp), %r15
jmp 0x6077f
xorl %esi, %esi
movb 0x206(%r15), %al
movb %al, 0x7(%rsp)
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rbx
testq %rbx, %rbx
movq %rbp, 0x40(%rsp)
movq %rdi, 0x10(%rsp)
je 0x60aeb
movq 0x20(%rdi), %rax
movslq %esi, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %r12
movq 0x38(%rsp), %rax
leal 0x1(%rax), %r9d
leaq 0xdc0b(%rip), %r10      # 0x6e3d0
movq %rsi, 0x68(%rsp)
movq %r12, 0x58(%rsp)
movl %r13d, 0x34(%rsp)
movl %r9d, 0x50(%rsp)
xorl %eax, %eax
movq %rax, 0x28(%rsp)
movq %rax, 0x20(%rsp)
testq %rbp, %rbp
je 0x6082b
movq 0x18(%rsp), %rax
movq (%rax), %rcx
movq 0x10(%rbx), %rax
movb (%rcx), %dl
testb %dl, %dl
je 0x6081c
incq %rcx
movzbl %dl, %edx
movb (%rdx,%r10), %sil
movzbl (%rax), %r8d
cmpb (%r8,%r10), %sil
jne 0x6081e
incq %rax
movb (%rcx), %dl
incq %rcx
testb %dl, %dl
jne 0x607ff
xorl %edx, %edx
movb (%rdx,%r10), %cl
movzbl (%rax), %eax
cmpb (%rax,%r10), %cl
jne 0x60846
cmpb $0x0, 0x206(%r15)
movq 0x10(%rbx), %rdx
je 0x60885
movq %rdx, %rsi
movq %r12, %rdx
callq 0x35497
jmp 0x60892
movslq 0x28(%rbx), %rax
testq %rax, %rax
jle 0x60ade
shlq $0x4, %rax
xorl %ecx, %ecx
movslq 0x40(%rbx,%rcx), %rdx
cmpl $0x0, (%rbp,%rdx,4)
jns 0x6082b
testl %r13d, %r13d
je 0x60877
movq 0x18(%rsp), %rsi
movswl 0x40(%rsi), %esi
cmpl %esi, %edx
je 0x6082b
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x60859
jmp 0x60ade
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rcx
callq 0x11d27
movq %rax, %r14
testq %rax, %rax
je 0x608ba
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
leaq 0x28(%rsp), %rcx
leaq 0x20(%rsp), %r8
callq 0x61aab
testl %eax, %eax
je 0x6096d
cmpb $0x0, 0x7(%rsp)
movq 0x10(%rsp), %rdi
je 0x60723
cmpb $0x0, 0x50(%rdi)
jne 0x60723
testq %r14, %r14
movl 0x50(%rsp), %r9d
leaq 0xdaed(%rip), %r10      # 0x6e3d0
jne 0x60ade
movq %r15, %rdi
movl %r9d, %r13d
callq 0x48791
movq %rax, %r14
movl 0x28(%rbx), %eax
testl %eax, %eax
jle 0x60936
movl 0x34(%r14), %ecx
leal (%rcx,%rax), %ebp
incl %ebp
leaq 0x40(%rbx), %r15
xorl %r12d, %r12d
movl (%r15), %edx
addl %r13d, %edx
movq %r14, %rdi
movl $0x4a, %esi
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x487d8
incq %r12
movslq 0x28(%rbx), %rax
addq $0x10, %r15
cmpq %rax, %r12
jl 0x6090e
movzbl 0x2c(%rbx), %edx
movq %r14, %rdi
movl $0x7d, %esi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x487d8
movq 0x40(%rsp), %rbp
movq 0x8(%rsp), %r15
movq 0x10(%rsp), %rdi
movq 0x58(%rsp), %r12
movl 0x34(%rsp), %r13d
jmp 0x60ad7
movq 0x20(%rsp), %rdx
testq %rdx, %rdx
jne 0x60983
movl 0x40(%rbx), %eax
movl %eax, 0x54(%rsp)
leaq 0x54(%rsp), %rdx
cmpl $0x0, 0x28(%rbx)
movq %rdx, 0x48(%rsp)
jle 0x60a2c
movq 0x28(%rsp), %r12
leaq 0x40(%r14), %rax
movq %rax, 0x70(%rsp)
xorl %r15d, %r15d
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rax
movswl 0x40(%rax), %eax
cmpl %eax, (%rdx,%r15,2)
jne 0x609c4
movl $0xffffffff, (%rdx,%r15,2) # imm = 0xFFFFFFFF
cmpq $0x0, 0x170(%rcx)
je 0x60a1a
movq 0x70(%rsp), %rax
testq %r12, %r12
je 0x609e0
movq 0x8(%r12), %rax
addq %r15, %rax
movq 0x8(%r14), %rcx
movswq (%rax), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq (%rcx,%rax), %rdx
movq (%r14), %rsi
movq 0x8(%rsp), %rdi
movq 0x68(%rsp), %rcx
callq 0x4f8fd
xorl %r13d, %r13d
cmpl $0x2, %eax
sete %r13b
movq 0x10(%rsp), %rcx
movq 0x48(%rsp), %rdx
incq %rbp
movslq 0x28(%rbx), %rax
addq $0x2, %r15
cmpq %rax, %rbp
jl 0x609ad
jmp 0x60a2f
xorl %r13d, %r13d
movl 0x3c(%r14), %edx
movq (%r14), %r8
movq 0x8(%rsp), %r15
movq %r15, %rdi
movq 0x68(%rsp), %r12
movl %r12d, %esi
xorl %ecx, %ecx
callq 0x48985
incl 0x54(%r15)
movq 0x38(%rsp), %rax
testl %eax, %eax
je 0x60a82
movq 0x28(%rsp), %rcx
subq $0x8, %rsp
movq %r15, %rdi
movl %r12d, %esi
movq %r14, %rdx
movq %rbx, %r8
movq 0x50(%rsp), %r9
pushq %r13
pushq $-0x1
pushq %rax
callq 0x61e7c
addq $0x20, %rsp
movq 0x60(%rsp), %rax
testl %eax, %eax
movq 0x40(%rsp), %rbp
je 0x60ab8
movq 0x28(%rsp), %rcx
subq $0x8, %rsp
movq %r15, %rdi
movl %r12d, %esi
movq %r14, %rdx
movq %rbx, %r8
movq 0x50(%rsp), %r9
pushq %r13
pushq $0x1
pushq %rax
callq 0x61e7c
addq $0x20, %rsp
movq 0x20(%rsp), %rsi
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x9b42
movq %r14, %rdi
movl 0x34(%rsp), %r13d
movq 0x58(%rsp), %r12
leaq 0xd8f2(%rip), %r10      # 0x6e3d0
movq 0x8(%rbx), %rbx
testq %rbx, %rbx
jne 0x607d9
movq 0x18(%rsp), %rdi
callq 0x52fb1
movq 0x10(%rsp), %r14
testq %rax, %rax
je 0x60723
movq %rax, %rbx
xorl %r15d, %r15d
leaq 0x20(%rsp), %r12
movq %r15, 0x28(%rsp)
movq %r15, 0x20(%rsp)
testq %rbp, %rbp
je 0x60b3d
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movl %r13d, %ecx
callq 0x60403
movq 0x10(%rsp), %r14
testl %eax, %eax
je 0x60c6b
cmpb $0x0, 0x2c(%rbx)
jne 0x60b68
testb $0x1, 0x2f(%r14)
jne 0x60b68
movq 0x8(%rsp), %rax
cmpq $0x0, 0x1e0(%rax)
jne 0x60b68
movq 0x8(%rsp), %rax
cmpb $0x0, 0x23(%rax)
je 0x60c6b
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rbx, %rdx
leaq 0x28(%rsp), %rcx
movq %r12, %r8
callq 0x61aab
testl %eax, %eax
je 0x60ba6
cmpb $0x0, 0x7(%rsp)
movq 0x10(%rsp), %r14
je 0x60723
cmpb $0x0, 0x50(%r14)
je 0x60c6b
jmp 0x60723
movq 0x10(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x3fa2e
testq %rax, %rax
je 0x60c59
movq %rax, %rbp
movq (%rbx), %rax
movq %rax, 0x28(%rbp)
movq (%rax), %rcx
movq %rcx, 0x18(%rbp)
incw 0x44(%rax)
movq 0x8(%rsp), %rdi
movl 0x54(%rdi), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x54(%rdi)
movl %eax, 0x44(%rbp)
movq 0x60(%rsp), %rax
testl %eax, %eax
je 0x60c12
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %r9
movq %rbp, %rsi
movq 0x18(%rsp), %rdx
movq %rbx, %r8
pushq $-0x1
pushq %rax
callq 0x62432
addq $0x10, %rsp
movq 0x38(%rsp), %rax
testl %eax, %eax
je 0x60c41
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %r9
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
movq 0x18(%rsp), %rdx
movq %rbx, %r8
pushq $0x1
pushq %rax
callq 0x62432
addq $0x10, %rsp
movq $0x0, 0x18(%rbp)
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x35d57
movq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x9b42
movq 0x40(%rsp), %rbp
movq 0x18(%rbx), %rbx
testq %rbx, %rbx
jne 0x60b0e
jmp 0x60723