// File: /workspace/github_c_makefile_star2_O3/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, %r12
movq %rcx, 0x28(%rsp)
movq %rdx, %r15
movq %rsi, 0x30(%rsp)
movq %rdi, %r14
movq (%rdi), %rax
movq %rax, 0x10(%rsp)
callq 0x48791
movq %rax, %rbx
xorl %eax, %eax
cmpl $0x0, 0xd8(%rsp)
jns 0x6248f
movzbl 0x2c(%r12), %edx
movq %rbx, %rdi
movl $0x7e, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x487d8
movq %r15, 0x20(%rsp)
movq %r14, 0x8(%rsp)
movl %eax, 0x54(%rsp)
movq %rbx, 0x58(%rsp)
cmpl $0x0, 0x28(%r12)
movq %r12, 0x40(%rsp)
jle 0x62586
leaq 0x40(%r12), %rax
movq %rax, 0x38(%rsp)
xorl %r12d, %r12d
movq 0x18(%rsp), %rbp
xorl %r13d, %r13d
movq 0x28(%rsp), %rdx
movq 0x8(%rsp), %r14
movq 0x40(%rsp), %rbx
movq 0x10(%rsp), %r15
testq %rdx, %rdx
movq %r13, 0x48(%rsp)
je 0x624f1
movq 0x8(%rdx), %rax
movzwl (%rax,%r12,2), %eax
jmp 0x624f5
movw $0xffff, %ax            # imm = 0xFFFF
movswl %ax, %ecx
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movl 0xd0(%rsp), %edx
callq 0x62877
movq %rax, %r13
cmpq $0x0, 0x18(%rsp)
movq %rbp, %rax
cmoveq 0x38(%rsp), %rax
movswq (%rax), %rax
movq (%rbx), %rcx
movq 0x8(%rcx), %rcx
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq (%rcx,%rax), %rdx
movq %r15, %rdi
movl $0x1b, %esi
callq 0x3f8d8
movq %r14, %rdi
movl $0x4d, %esi
movq %r13, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x3bc7d
movq %r15, %rdi
movq 0x48(%rsp), %rsi
movq %rax, %rdx
callq 0x5150c
movq %rax, %r13
incq %r12
movslq 0x28(%rbx), %rax
addq $0x4, %rbp
cmpq %rax, %r12
movq 0x28(%rsp), %rdx
jl 0x624dc
jmp 0x6258e
xorl %r13d, %r13d
movq 0x28(%rsp), %rdx
movl 0xd8(%rsp), %r12d
testl %r12d, %r12d
movq 0x10(%rsp), %r15
jle 0x62663
movq 0x40(%rsp), %rax
movq 0x20(%rsp), %rcx
cmpq %rcx, (%rax)
movq 0x8(%rsp), %r14
jne 0x62796
testb $0x20, 0x48(%rcx)
jne 0x6266d
movq (%r14), %rdi
leaq 0x60(%rsp), %rdx
movq $0x0, (%rdx)
movl $0x0, 0x8(%rdx)
movl $0x85, %esi
xorl %ecx, %ecx
callq 0x51708
movq %rax, %r15
testq %rax, %rax
je 0x62600
movl 0xd0(%rsp), %eax
movl %eax, 0x2c(%r15)
movb $0x64, 0x1(%r15)
movq 0x30(%rsp), %rax
movl 0x44(%rax), %ebp
leaq 0x60(%rsp), %rdx
movq $0x0, (%rdx)
movl $0x0, 0x8(%rdx)
movq 0x10(%rsp), %rdi
movl $0x99, %esi
xorl %ecx, %ecx
callq 0x51708
testq %rax, %rax
je 0x62643
movq 0x20(%rsp), %rcx
movq %rcx, 0x40(%rax)
movl %ebp, 0x2c(%rax)
movw $0xffff, 0x30(%rax)     # imm = 0xFFFF
movq %r14, %rdi
movl $0x4c, %esi
movq %r15, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x3bc7d
movq 0x10(%rsp), %r15
jmp 0x62785
movq 0x8(%rsp), %r14
jmp 0x62796
leaq 0x10(%rcx), %rax
movq (%rax), %r14
movb 0x63(%r14), %cl
andb $0x3, %cl
leaq 0x28(%r14), %rax
cmpb $0x2, %cl
jne 0x62671
movq %r13, 0x48(%rsp)
cmpw $0x0, 0x5e(%r14)
je 0x62759
xorl %r12d, %r12d
xorl %ebx, %ebx
movq 0x8(%rsp), %rbp
movq 0x8(%rdx), %rax
movzwl (%rax,%r12,2), %r13d
movswl %r13w, %ecx
movq %rbp, %rdi
movq 0x20(%rsp), %rbp
movq %rbp, %rsi
movl 0xd0(%rsp), %edx
callq 0x62877
movq %rax, 0x18(%rsp)
movq 0x30(%rsp), %rax
movl 0x44(%rax), %eax
movl %eax, 0x38(%rsp)
movq $0x0, 0x60(%rsp)
movl $0x0, 0x68(%rsp)
movq %r15, %rdi
movl $0x99, %esi
leaq 0x60(%rsp), %rdx
xorl %ecx, %ecx
callq 0x51708
testq %rax, %rax
je 0x6270e
movq %rbp, 0x40(%rax)
movl 0x38(%rsp), %ecx
movl %ecx, 0x2c(%rax)
movw %r13w, 0x30(%rax)
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
movl $0x4d, %esi
movq 0x18(%rsp), %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x3bc7d
movq 0x10(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x5150c
movq %rax, %rbx
incq %r12
movzwl 0x5e(%r14), %eax
cmpq %rax, %r12
movq 0x28(%rsp), %rdx
jb 0x6269f
jmp 0x62760
xorl %ebx, %ebx
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
movl $0x13, %esi
movq %rbx, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3bc7d
movl 0xd8(%rsp), %r12d
movq %rbp, %r14
movq 0x48(%rsp), %r13
movq %r15, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x5150c
movq %rax, %r13
testl %r12d, %r12d
setg %bpl
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rdi
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movq $0x0, 0x30(%rdi)
movq 0x30(%rsp), %rbx
movq %rbx, 0x8(%rdi)
movq %r14, (%rdi)
movq %r13, %rsi
callq 0x4df86
movl $0x0, (%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x55114
movq %rax, %r15
movq 0x40(%rsp), %rax
movb 0x2c(%rax), %al
testb %al, %al
sete %cl
andb %bpl, %cl
cmpb $0x1, %cl
jne 0x62812
movq 0x1e0(%r14), %rax
testq %rax, %rax
cmoveq %r14, %rax
movb $0x1, 0x24(%rax)
xorl %eax, %eax
movzbl %al, %edx
movq 0x58(%rsp), %r14
movq %r14, %rdi
movl $0x7d, %esi
movl %r12d, %ecx
xorl %r8d, %r8d
callq 0x487d8
testq %r15, %r15
movl 0x54(%rsp), %ebp
je 0x6283e
movq %r15, %rdi
callq 0x5823e
movq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x35bca
testl %ebp, %ebp
jle 0x62865
movl 0x34(%r14), %eax
cmpl %ebp, %eax
jbe 0x62865
movq 0x8(%r14), %rcx
movl %ebp, %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