// File: /workspace/github_c_makefile_star2_O2/skeeto[P]flappy/sqlite3.c
// Function: fkScanChildren
static void fkScanChildren(
  Parse *pParse,                  /* Parse context */
  SrcList *pSrc,                  /* The child table to be scanned */
  Table *pTab,                    /* The parent table */
  Index *pIdx,                    /* Index on parent covering the foreign key */
  FKey *pFKey,                    /* The foreign key linking pSrc to pTab */
  int *aiCol,                     /* Map from pIdx cols to child table cols */
  int regData,                    /* Parent row data starts here */
  int nIncr                       /* Amount to increment deferred counter by */
){
  sqlite3 *db = pParse->db;       /* Database handle */
  int i;                          /* Iterator variable */
  Expr *pWhere = 0;               /* WHERE clause to scan with */
  NameContext sNameContext;       /* Context used to resolve WHERE clause */
  WhereInfo *pWInfo;              /* Context used by sqlite3WhereXXX() */
  int iFkIfZero = 0;              /* Address of OP_FkIfZero */
  Vdbe *v = sqlite3GetVdbe(pParse);

  assert( pIdx==0 || pIdx->pTable==pTab );
  assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
  assert( pIdx!=0 || pFKey->nCol==1 );
  assert( pIdx!=0 || HasRowid(pTab) );

  if( nIncr<0 ){
    iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
  }

  /* Create an Expr object representing an SQL expression like:
  **
  **   <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
  **
  ** The collation sequence used for the comparison should be that of
  ** the parent key columns. The affinity of the parent key column should
  ** be applied to each child key value before the comparison takes place.
  */
  for(i=0; i<pFKey->nCol; i++){
    Expr *pLeft;                  /* Value from parent table row */
    Expr *pRight;                 /* Column ref to child table */
    Expr *pEq;                    /* Expression (pLeft = pRight) */
    i16 iCol;                     /* Index of column in child table */ 
    const char *zCol;             /* Name of column in child table */

    iCol = pIdx ? pIdx->aiColumn[i] : -1;
    pLeft = exprTableRegister(pParse, pTab, regData, iCol);
    iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
    assert( iCol>=0 );
    zCol = pFKey->pFrom->aCol[iCol].zName;
    pRight = sqlite3Expr(db, TK_ID, zCol);
    pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
    pWhere = sqlite3ExprAnd(db, pWhere, pEq);
  }

  /* If the child table is the same as the parent table, then add terms
  ** to the WHERE clause that prevent this entry from being scanned.
  ** The added WHERE clause terms are like this:
  **
  **     $current_rowid!=rowid
  **     NOT( $current_a==a AND $current_b==b AND ... )
  **
  ** The first form is used for rowid tables.  The second form is used
  ** for WITHOUT ROWID tables.  In the second form, the primary key is
  ** (a,b,...)
  */
  if( pTab==pFKey->pFrom && nIncr>0 ){
    Expr *pNe;                    /* Expression (pLeft != pRight) */
    Expr *pLeft;                  /* Value from parent table row */
    Expr *pRight;                 /* Column ref to child table */
    if( HasRowid(pTab) ){
      pLeft = exprTableRegister(pParse, pTab, regData, -1);
      pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
      pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
    }else{
      Expr *pEq, *pAll = 0;
      Index *pPk = sqlite3PrimaryKeyIndex(pTab);
      assert( pIdx!=0 );
      for(i=0; i<pPk->nKeyCol; i++){
        i16 iCol = pIdx->aiColumn[i];
        pLeft = exprTableRegister(pParse, pTab, regData, iCol);
        pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
        pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
        pAll = sqlite3ExprAnd(db, pAll, pEq);
      }
      pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0, 0);
    }
    pWhere = sqlite3ExprAnd(db, pWhere, pNe);
  }

  /* Resolve the references in the WHERE clause. */
  memset(&sNameContext, 0, sizeof(NameContext));
  sNameContext.pSrcList = pSrc;
  sNameContext.pParse = pParse;
  sqlite3ResolveExprNames(&sNameContext, pWhere);

  /* Create VDBE to loop through the entries in pSrc that match the WHERE
  ** clause. If the constraint is not deferred, throw an exception for
  ** each row found. Otherwise, for deferred constraints, increment the
  ** deferred constraint counter by nIncr for each row selected.  */
  pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
  if( nIncr>0 && pFKey->isDeferred==0 ){
    sqlite3ParseToplevel(pParse)->mayAbort = 1;
  }
  sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
  if( pWInfo ){
    sqlite3WhereEnd(pWInfo);
  }

  /* Clean up the WHERE clause constructed above. */
  sqlite3ExprDelete(db, pWhere);
  if( iFkIfZero ){
    sqlite3VdbeJumpHere(v, iFkIfZero);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, %r13
movq %r8, %r12
movq %rcx, %rbx
movq %rdx, 0x40(%rsp)
movq %rsi, 0x20(%rsp)
movq (%rdi), %rax
movq %rax, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
callq 0x43f1b
movq %rax, %r14
movl $0x0, 0x1c(%rsp)
cmpl $0x0, 0xd8(%rsp)
jns 0x57983
movzbl 0x2c(%r12), %edx
pushq $0x7e
popq %rsi
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x43f64
movl %eax, 0x1c(%rsp)
movq %r14, 0x58(%rsp)
leaq 0x40(%r12), %rax
movq %rax, 0x28(%rsp)
xorl %r14d, %r14d
movq %r13, 0x30(%rsp)
xorl %eax, %eax
movq %r12, 0x38(%rsp)
movq %rbx, 0x48(%rsp)
movslq 0x28(%r12), %rcx
cmpq %rcx, %r14
jge 0x57a55
movq %rax, %r15
testq %rbx, %rbx
je 0x579c7
movq 0x8(%rbx), %rax
movzwl (%rax,%r14,2), %eax
jmp 0x579cb
movw $0xffff, %ax            # imm = 0xFFFF
movswl %ax, %ecx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq 0x40(%rsp), %rsi
movl 0xd0(%rsp), %edx
callq 0x57c90
movq %rax, %rbp
cmpq $0x0, 0x30(%rsp)
movq %r13, %rax
cmoveq 0x28(%rsp), %rax
movswq (%rax), %rax
movq (%r12), %rcx
movq 0x8(%rcx), %rcx
imulq $0x30, %rax, %rax
movq (%rcx,%rax), %rdx
movq 0x10(%rsp), %r12
movq %r12, %rdi
pushq $0x1b
popq %rsi
callq 0x3cb67
movq %rbx, %rdi
pushq $0x4d
popq %rsi
movq %rbp, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x37d23
movq %r12, %rdi
movq 0x38(%rsp), %r12
movq %r15, %rsi
movq %rax, %rdx
callq 0x4b411
incq %r14
addq $0x4, %r13
movq 0x48(%rsp), %rbx
jmp 0x579a6
cmpl $0x0, 0xd8(%rsp)
movq 0x40(%rsp), %r15
jle 0x57ad0
cmpq %r15, (%r12)
movq 0x10(%rsp), %r14
jne 0x57ad5
movq %rax, 0x50(%rsp)
testb $0x20, 0x48(%r15)
jne 0x57adf
pushq $-0x1
popq %rbx
movq 0x8(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movl 0xd0(%rsp), %edx
movl %ebx, %ecx
callq 0x57c90
movq %rax, %r13
movq 0x20(%rsp), %rax
movl 0x44(%rax), %edx
movq %r14, %rdi
movq %r15, %rsi
movl %ebx, %ecx
movq %r12, %rbx
movq 0x38(%rsp), %r12
callq 0x57d32
pushq $0x4c
popq %rsi
movq %rbx, %rdi
movq %r13, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x37d23
jmp 0x57b9b
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %rbx
jmp 0x57bab
movq %r15, %rdi
callq 0x30033
movq %rax, 0x28(%rsp)
xorl %r14d, %r14d
xorl %edx, %edx
movq 0x40(%rsp), %r15
movq 0x28(%rsp), %rax
movzwl 0x5e(%rax), %eax
cmpq %rax, %r14
jae 0x57b81
movq 0x8(%rbx), %rax
movswl (%rax,%r14,2), %ebp
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %rdx, 0x30(%rsp)
movl 0xd0(%rsp), %edx
movl %ebp, %ecx
callq 0x57c90
movq %rax, %r13
movq 0x20(%rsp), %rax
movl 0x44(%rax), %edx
movq 0x10(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movl %ebp, %ecx
callq 0x57d32
movq %rbx, %rdi
movq 0x48(%rsp), %rbx
pushq $0x4d
popq %rsi
movq %r13, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x37d23
movq %r12, %rdi
movq 0x38(%rsp), %r12
movq 0x30(%rsp), %rsi
movq %rax, %rdx
callq 0x4b411
movq %rax, %rdx
incq %r14
jmp 0x57af6
pushq $0x13
popq %rsi
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x37d23
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq 0x50(%rsp), %rsi
movq %rax, %rdx
callq 0x4b411
cmpl $0x0, 0xd8(%rsp)
setg %bpl
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rdi
andq $0x0, 0x30(%rdi)
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movq 0x20(%rsp), %r15
movq %r15, 0x8(%rdi)
movq %rbx, (%rdi)
movq %rax, %r13
movq %rax, %rsi
callq 0x48812
andl $0x0, (%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %r15
movq %r13, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x4cc2b
movq %rbx, %rdx
movq %rax, %rbx
movb 0x2c(%r12), %al
testb %al, %al
sete %cl
andb %bpl, %cl
cmpb $0x1, %cl
jne 0x57c2c
movq 0x1e0(%rdx), %rax
testq %rax, %rax
cmoveq %rdx, %rax
movb $0x1, 0x24(%rax)
xorl %eax, %eax
movzbl %al, %edx
pushq $0x7d
popq %rsi
movq 0x58(%rsp), %r12
movq %r12, %rdi
movl 0xd8(%rsp), %ecx
callq 0x43f64
testq %rbx, %rbx
je 0x57c53
movq %rbx, %rdi
callq 0x4fb43
movq %r14, %rdi
movq %r15, %rsi
callq 0x3044f
movl 0x1c(%rsp), %edx
testl %edx, %edx
jle 0x57c7e
movl 0x34(%r12), %eax
cmpl %edx, %eax
jbe 0x57c7e
movq 0x8(%r12), %rcx
movl %edx, %edx
imulq $0x18, %rdx, %rdx
movl %eax, 0x8(%rcx,%rdx)
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq