// File: /workspace/github_c_makefile_star2_O1/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, 0x18(%rsp)
movq %r8, %r15
movq %rcx, 0x50(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, %r12
movq (%rdi), %r14
callq 0x47f6a
movq %rax, %rbx
movl $0x0, 0x14(%rsp)
cmpl $0x0, 0xd8(%rsp)
jns 0x61d85
movzbl 0x2c(%r15), %edx
movq %rbx, %rdi
movl $0x7e, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x47fb1
movl %eax, 0x14(%rsp)
movq %r12, 0x8(%rsp)
movq %r14, 0x30(%rsp)
movq %rbx, 0x58(%rsp)
cmpl $0x0, 0x28(%r15)
movq %r15, 0x20(%rsp)
jle 0x61e70
leaq 0x40(%r15), %rax
movq %rax, 0x40(%rsp)
xorl %r13d, %r13d
movq 0x18(%rsp), %rbp
xorl %ebx, %ebx
movq 0x20(%rsp), %r14
movq 0x30(%rsp), %r12
movq 0x50(%rsp), %rax
testq %rax, %rax
movq %rbx, 0x48(%rsp)
je 0x61ddb
movq 0x8(%rax), %rax
movzwl (%rax,%r13,2), %eax
jmp 0x61ddf
movw $0xffff, %ax            # imm = 0xFFFF
movq 0x28(%rsp), %rsi
movswl %ax, %ecx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movl 0xd0(%rsp), %edx
callq 0x6215b
movq %rax, %r15
cmpq $0x0, 0x18(%rsp)
movq %rbp, %rax
cmoveq 0x40(%rsp), %rax
movswq (%rax), %rax
movq (%r14), %rcx
movq 0x8(%rcx), %rcx
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq (%rcx,%rax), %rdx
movq %r12, %rdi
movl $0x1b, %esi
callq 0x3f0ca
movq %rbx, %rdi
movl $0x4d, %esi
movq %r15, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x3b872
movq %r12, %rdi
movq 0x48(%rsp), %rsi
movq %rax, %rdx
callq 0x50efd
movq %rax, %rbx
incq %r13
movslq 0x28(%r14), %rax
addq $0x4, %rbp
cmpq %rax, %r13
jl 0x61dc1
jmp 0x61e72
xorl %ebx, %ebx
movl 0xd8(%rsp), %ebp
testl %ebp, %ebp
jle 0x61f3e
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %r14
cmpq %r14, (%r13)
movq 0x30(%rsp), %r15
jne 0x61f48
testb $0x20, 0x48(%r14)
jne 0x61f50
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x60(%rsp), %rdx
movq $0x0, (%rdx)
movl $0x0, 0x8(%rdx)
xorl %r12d, %r12d
movl $0x85, %esi
xorl %ecx, %ecx
callq 0x510f7
testq %rax, %rax
je 0x61ee5
movl 0xd0(%rsp), %ecx
movl %ecx, 0x2c(%rax)
movb $0x64, 0x1(%rax)
movq %rax, %r12
movq 0x38(%rsp), %rax
movl 0x44(%rax), %ebp
leaq 0x60(%rsp), %rdx
movq $0x0, (%rdx)
movl $0x0, 0x8(%rdx)
movq %r15, %rdi
movl $0x99, %esi
xorl %ecx, %ecx
callq 0x510f7
testq %rax, %rax
je 0x61f21
movq %r14, 0x40(%rax)
movl %ebp, 0x2c(%rax)
movw $0xffff, 0x30(%rax)     # imm = 0xFFFF
movq 0x8(%rsp), %rdi
movl $0x4c, %esi
movq %r12, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x3b872
jmp 0x62066
movq 0x30(%rsp), %r15
movq 0x20(%rsp), %r13
movq %rbx, %r14
jmp 0x6207e
movq 0x10(%r14), %rcx
testq %rcx, %rcx
je 0x61f72
movq 0x8(%rsp), %r13
movb 0x63(%rcx), %al
andb $0x3, %al
cmpb $0x2, %al
je 0x61f77
movq 0x28(%rcx), %rcx
testq %rcx, %rcx
jne 0x61f5e
jmp 0x61f77
movq 0x8(%rsp), %r13
cmpw $0x0, 0x5e(%rcx)
je 0x6204a
xorl %r12d, %r12d
xorl %ebp, %ebp
movq %rcx, 0x40(%rsp)
movq 0x28(%rsp), %r14
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rax
movq %r13, %rdi
movzwl (%rax,%r12,2), %r13d
movswl %r13w, %ecx
movq %r14, %rsi
movl 0xd0(%rsp), %edx
callq 0x6215b
movq %rax, 0x48(%rsp)
movq 0x38(%rsp), %rax
movl 0x44(%rax), %eax
movl %eax, 0x18(%rsp)
movq $0x0, 0x60(%rsp)
movl $0x0, 0x68(%rsp)
movq %r15, %rdi
movl $0x99, %esi
leaq 0x60(%rsp), %rdx
xorl %ecx, %ecx
callq 0x510f7
testq %rax, %rax
je 0x62000
movq %r14, 0x40(%rax)
movl 0x18(%rsp), %ecx
movl %ecx, 0x2c(%rax)
movw %r13w, 0x30(%rax)
movq 0x8(%rsp), %r13
movq %r13, %rdi
movl $0x4d, %esi
movq 0x48(%rsp), %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x3b872
movq 0x30(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x50efd
movq %rax, %rbp
incq %r12
movq 0x40(%rsp), %rcx
movzwl 0x5e(%rcx), %eax
cmpq %rax, %r12
jb 0x61f91
jmp 0x6204c
xorl %ebp, %ebp
movq %r13, %rdi
movl $0x13, %esi
movq %rbp, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3b872
movq 0x20(%rsp), %r13
movq %r15, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x50efd
movq %rax, %r14
movl 0xd8(%rsp), %ebp
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rdi
movaps %xmm0, (%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movq $0x0, 0x30(%rdi)
movq 0x38(%rsp), %r12
movq %r12, 0x8(%rdi)
movq 0x8(%rsp), %rbx
movq %rbx, (%rdi)
movq %r14, %rsi
callq 0x4d8f9
movl $0x0, (%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x54ae1
movq %rax, %r12
testl %ebp, %ebp
movq %r14, %rbx
jle 0x620f7
cmpb $0x0, 0x2c(%r13)
jne 0x620f7
movq 0x8(%rsp), %rcx
movq 0x1e0(%rcx), %rax
testq %rax, %rax
cmoveq %rcx, %rax
movb $0x1, 0x24(%rax)
movzbl 0x2c(%r13), %edx
movq 0x58(%rsp), %r14
movq %r14, %rdi
movl $0x7d, %esi
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x47fb1
testq %r12, %r12
je 0x62120
movq %r12, %rdi
callq 0x57b81
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3572a
movl 0x14(%rsp), %edx
testl %edx, %edx
jle 0x62149
movl 0x34(%r14), %eax
cmpl %edx, %eax
jbe 0x62149
movq 0x8(%r14), %rcx
movl %edx, %edx
leaq (%rdx,%rdx,2), %rdx
movl %eax, 0x8(%rcx,%rdx,8)
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq