// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movl %r9d, -0x24(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movzbl 0x206(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x30(%rbp), %rax
movl 0x2c(%rax), %eax
andl $0x80000, %eax          # imm = 0x80000
cmpl $0x0, %eax
jne 0x84930
jmp 0x84edf
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rsi
callq 0x16b60
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x3c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x84cce
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movl $0x0, -0x7c(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x849ca
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0xf670
cmpl $0x0, %eax
je 0x849ca
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl -0x24(%rbp), %ecx
callq 0x84430
cmpl $0x0, %eax
jne 0x849ca
jmp 0x84cbd
movq -0x8(%rbp), %rax
cmpb $0x0, 0x206(%rax)
je 0x849f2
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x48(%rbp), %rdx
callq 0x4af30
movq %rax, -0x58(%rbp)
jmp 0x84a0d
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0x48(%rbp), %rcx
xorl %esi, %esi
callq 0x16840
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x84a36
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x38(%rbp), %rdx
leaq -0x60(%rbp), %rcx
leaq -0x68(%rbp), %r8
callq 0x859d0
cmpl $0x0, %eax
je 0x84b04
cmpl $0x0, -0x4c(%rbp)
je 0x84a49
movq -0x30(%rbp), %rax
movzbl 0x50(%rax), %eax
cmpl $0x0, %eax
je 0x84a4e
jmp 0x84edf
cmpq $0x0, -0x58(%rbp)
jne 0x84aff
movq -0x8(%rbp), %rdi
callq 0x638c0
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
callq 0x63ea0
movq -0x38(%rbp), %rcx
addl 0x28(%rcx), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jge 0x84ae1
movq -0x38(%rbp), %rax
addq $0x40, %rax
movslq -0x78(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
addl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
movl -0x90(%rbp), %edx
movl -0x8c(%rbp), %ecx
movl $0x4a, %esi
callq 0x63980
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x84a8c
movq -0x88(%rbp), %rdi
movq -0x38(%rbp), %rax
movzbl 0x2c(%rax), %edx
movl $0x7d, %esi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x63980
jmp 0x84cbd
cmpq $0x0, -0x68(%rbp)
je 0x84b15
movq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x84b27
movq -0x38(%rbp), %rax
movl 0x40(%rax), %eax
movl %eax, -0x74(%rbp)
leaq -0x74(%rbp), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jge 0x84c15
movq -0x70(%rbp), %rax
movslq -0x78(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
movswl 0x40(%rcx), %ecx
cmpl %ecx, %eax
jne 0x84b64
movq -0x70(%rbp), %rax
movslq -0x78(%rbp), %rcx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movq -0x30(%rbp), %rax
cmpq $0x0, 0x170(%rax)
je 0x84c05
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x84ba4
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x78(%rbp), %rcx
movswl (%rax,%rcx,2), %eax
movl %eax, -0xcc(%rbp)
jmp 0x84bb2
movq -0x58(%rbp), %rax
movswl 0x40(%rax), %eax
movl %eax, -0xcc(%rbp)
movq -0xc8(%rbp), %rax
movl -0xcc(%rbp), %ecx
movslq %ecx, %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movq (%rax), %rsi
movq -0xa0(%rbp), %rdx
movl -0x3c(%rbp), %ecx
callq 0x6dcf0
movl %eax, -0x94(%rbp)
cmpl $0x2, -0x94(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x7c(%rbp)
jmp 0x84c07
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x84b2e
movq -0x8(%rbp), %rdi
movl -0x3c(%rbp), %esi
movq -0x58(%rbp), %rax
movl 0x3c(%rax), %edx
movq -0x58(%rbp), %rax
movq (%rax), %r8
xorl %ecx, %ecx
callq 0x63f00
movq -0x8(%rbp), %rax
movl 0x54(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x54(%rax)
cmpl $0x0, -0x14(%rbp)
je 0x84c77
movq -0x8(%rbp), %rdi
movl -0x3c(%rbp), %esi
movq -0x58(%rbp), %rdx
movq -0x60(%rbp), %rcx
movq -0x38(%rbp), %r8
movq -0x70(%rbp), %r9
movl -0x14(%rbp), %r10d
movl -0x7c(%rbp), %eax
movl %r10d, (%rsp)
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
movl %eax, 0x10(%rsp)
callq 0x85de0
cmpl $0x0, -0x18(%rbp)
je 0x84cb0
movq -0x8(%rbp), %rdi
movl -0x3c(%rbp), %esi
movq -0x58(%rbp), %rdx
movq -0x60(%rbp), %rcx
movq -0x38(%rbp), %r8
movq -0x70(%rbp), %r9
movl -0x18(%rbp), %r10d
movl -0x7c(%rbp), %eax
movl %r10d, (%rsp)
movl $0x1, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x85de0
movq -0x30(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0xc760
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x8496a
movq -0x10(%rbp), %rdi
callq 0x726e0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x84edf
movq $0x0, -0xa8(%rbp)
movq $0x0, -0xb8(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x84d21
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl -0x24(%rbp), %ecx
callq 0x844c0
cmpl $0x0, %eax
jne 0x84d21
jmp 0x84ece
movq -0x38(%rbp), %rax
cmpb $0x0, 0x2c(%rax)
jne 0x84d59
movq -0x30(%rbp), %rax
movl 0x2c(%rax), %eax
andl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x0, %eax
jne 0x84d59
movq -0x8(%rbp), %rax
cmpq $0x0, 0x1e0(%rax)
jne 0x84d59
movq -0x8(%rbp), %rax
cmpb $0x0, 0x23(%rax)
jne 0x84d59
jmp 0x84ece
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
leaq -0xa8(%rbp), %rcx
leaq -0xb8(%rbp), %r8
callq 0x859d0
cmpl $0x0, %eax
je 0x84d9a
cmpl $0x0, -0x4c(%rbp)
je 0x84d90
movq -0x30(%rbp), %rax
movzbl 0x50(%rax), %eax
cmpl $0x0, %eax
je 0x84d95
jmp 0x84edf
jmp 0x84ece
movq -0x30(%rbp), %rdi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x596b0
movq %rax, -0xb0(%rbp)
cmpq $0x0, -0xb0(%rbp)
je 0x84ebe
movq -0xb0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xc0(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rcx
movq -0xc0(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rcx
movq -0xc0(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0xc0(%rbp), %rax
movq 0x20(%rax), %rax
movw 0x44(%rax), %cx
addw $0x1, %cx
movw %cx, 0x44(%rax)
movq -0x8(%rbp), %rax
movl 0x54(%rax), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x54(%rax)
movq -0xc0(%rbp), %rax
movl %ecx, 0x3c(%rax)
cmpl $0x0, -0x18(%rbp)
je 0x84e65
movq -0x8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0xa8(%rbp), %rcx
movq -0x38(%rbp), %r8
movq -0xb8(%rbp), %r9
movl -0x18(%rbp), %eax
movl %eax, (%rsp)
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
callq 0x86260
cmpl $0x0, -0x14(%rbp)
je 0x84e9f
movq -0x8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0xa8(%rbp), %rcx
movq -0x38(%rbp), %r8
movq -0xb8(%rbp), %r9
movl -0x14(%rbp), %eax
movl %eax, (%rsp)
movl $0x1, 0x8(%rsp)
callq 0x86260
movq -0xc0(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x30(%rbp), %rdi
movq -0xb0(%rbp), %rsi
callq 0x4bbe0
movq -0x30(%rbp), %rdi
movq -0xb8(%rbp), %rsi
callq 0xc760
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x84cdb
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax,%rax)