// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: fkScanChildren
static void fkScanChildren(
  Parse *pParse,                  /* Parse context */
  SrcList *pSrc,                  /* SrcList containing the table to scan */
  Table *pTab,
  Index *pIdx,                    /* Foreign key index */
  FKey *pFKey,                    /* Foreign key relationship */
  int *aiCol,                     /* Map from pIdx cols to child table cols */
  int regData,                    /* Referenced table 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 || pIdx->pTable==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) */
    int iCol;                     /* Index of column in child table */ 
    const char *zCol;             /* Name of column in child table */

    pLeft = sqlite3Expr(db, TK_REGISTER, 0);
    if( pLeft ){
      /* Set the collation sequence and affinity of the LHS of each TK_EQ
      ** expression to the parent key column defaults.  */
      if( pIdx ){
        Column *pCol;
        const char *zColl;
        iCol = pIdx->aiColumn[i];
        pCol = &pTab->aCol[iCol];
        if( pTab->iPKey==iCol ) iCol = -1;
        pLeft->iTable = regData+iCol+1;
        pLeft->affinity = pCol->affinity;
        zColl = pCol->zColl;
        if( zColl==0 ) zColl = db->pDfltColl->zName;
        pLeft = sqlite3ExprAddCollateString(pParse, pLeft, zColl);
      }else{
        pLeft->iTable = regData;
        pLeft->affinity = SQLITE_AFF_INTEGER;
      }
    }
    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, and this scan
  ** is taking place as part of a DELETE operation (operation D.2), omit the
  ** row being deleted from the scan by adding ($rowid != rowid) to the WHERE 
  ** clause, where $rowid is the rowid of the row being deleted.  */
  if( pTab==pFKey->pFrom && nIncr>0 ){
    Expr *pEq;                    /* Expression (pLeft = pRight) */
    Expr *pLeft;                  /* Value from parent table row */
    Expr *pRight;                 /* Column ref to child table */
    pLeft = sqlite3Expr(db, TK_REGISTER, 0);
    pRight = sqlite3Expr(db, TK_COLUMN, 0);
    if( pLeft && pRight ){
      pLeft->iTable = regData;
      pLeft->affinity = SQLITE_AFF_INTEGER;
      pRight->iTable = pSrc->a[0].iCursor;
      pRight->iColumn = -1;
    }
    pEq = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
    pWhere = sqlite3ExprAnd(db, pWhere, pEq);
  }

  /* 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, %r15
movq %r8, %r12
movq %rcx, -0x60(%rbp)
movq %rdx, -0x48(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdi, %r13
movl 0x10(%rbp), %r14d
movq (%rdi), %rax
movq %rax, -0x30(%rbp)
callq 0x41ef4
movq %rax, %rbx
xorl %eax, %eax
cmpl $0x0, 0x18(%rbp)
jns 0x55016
movzbl 0x2c(%r12), %edx
pushq $0x7c
popq %rsi
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x41f42
movl %eax, -0x34(%rbp)
movq %rbx, -0x58(%rbp)
leal 0x1(%r14), %eax
movl %eax, -0x38(%rbp)
leaq 0x40(%r12), %rax
movq %rax, -0x68(%rbp)
xorl %ebx, %ebx
movq %r15, -0x70(%rbp)
xorl %r14d, %r14d
movq %r13, -0x50(%rbp)
movslq 0x28(%r12), %rax
cmpq %rax, %rbx
jge 0x55139
movq %r14, -0x78(%rbp)
movq %r12, %r14
movq -0x30(%rbp), %rdi
movl $0x84, %esi
xorl %edx, %edx
callq 0x3b590
movl $0x0, %r13d
testq %rax, %rax
movq -0x70(%rbp), %r12
je 0x550d4
movq -0x60(%rbp), %rcx
testq %rcx, %rcx
je 0x550c7
movq 0x8(%rcx), %rcx
movslq (%rcx,%rbx,4), %rcx
movq -0x48(%rbp), %rdi
movq 0x8(%rdi), %rdx
imulq $0x30, %rcx, %rsi
movswl 0x40(%rdi), %edi
cmpl %edi, %ecx
pushq $-0x1
popq %rdi
cmovel %edi, %ecx
addl -0x38(%rbp), %ecx
movl %ecx, 0x2c(%rax)
movb 0x29(%rdx,%rsi), %cl
movb %cl, 0x1(%rax)
movq 0x20(%rdx,%rsi), %rdx
testq %rdx, %rdx
jne 0x550b9
movq -0x30(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq (%rcx), %rdx
movq -0x50(%rbp), %rdi
movq %rax, %rsi
callq 0x440df
jmp 0x550d1
movl 0x10(%rbp), %ecx
movl %ecx, 0x2c(%rax)
movb $0x64, 0x1(%rax)
movq %rax, %r13
testq %r12, %r12
movq %r15, %rax
cmoveq -0x68(%rbp), %rax
movslq (%rax), %rax
movq (%r14), %rcx
movq 0x8(%rcx), %rcx
imulq $0x30, %rax, %rax
movq (%rcx,%rax), %rdx
movq -0x30(%rbp), %r12
movq %r12, %rdi
pushq $0x1a
popq %rsi
callq 0x3b590
movq -0x50(%rbp), %rdi
pushq $0x4c
popq %rsi
movq %r13, %rdx
movq %rdi, %r13
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x36a09
movq %r12, %rdi
movq -0x78(%rbp), %rsi
movq %rax, %rdx
callq 0x45ef0
movq %r14, %r12
movq %rax, %r14
incq %rbx
addq $0x4, %r15
jmp 0x5503a
cmpl $0x0, 0x18(%rbp)
jle 0x551cc
movq -0x48(%rbp), %rax
movq %r12, %r15
cmpq %rax, (%r12)
jne 0x551cf
movq %r14, %r12
movq -0x30(%rbp), %r14
movq %r14, %rdi
movl $0x84, %esi
xorl %edx, %edx
callq 0x3b590
movq %rax, %rbx
movq %r14, %rdi
movl $0x98, %esi
xorl %edx, %edx
callq 0x3b590
testq %rbx, %rbx
setne %cl
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x551a4
movl 0x10(%rbp), %ecx
movl %ecx, 0x2c(%rbx)
movb $0x64, 0x1(%rbx)
movq -0x40(%rbp), %rcx
movl 0x44(%rcx), %ecx
movl %ecx, 0x2c(%rax)
orw $-0x1, 0x30(%rax)
pushq $0x4b
popq %rsi
movq %r13, %rdi
movq %rbx, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x36a09
movq -0x30(%rbp), %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x45ef0
movq %rax, %r14
jmp 0x551cf
movq %r12, %r15
cmpl $0x0, 0x18(%rbp)
setg %r12b
xorps %xmm0, %xmm0
leaq -0xb0(%rbp), %rdi
andq $0x0, 0x30(%rdi)
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movq -0x40(%rbp), %rbx
movq %rbx, 0x8(%rdi)
movq %r13, (%rdi)
movq %r14, %rsi
callq 0x42880
andl $0x0, (%rsp)
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x4a969
movq %rax, %rbx
movb 0x2c(%r15), %al
testb %al, %al
sete %cl
andb %r12b, %cl
cmpb $0x1, %cl
jne 0x55243
movq 0x1d8(%r13), %rax
testq %rax, %rax
cmoveq %r13, %rax
movb $0x1, 0x24(%rax)
xorl %eax, %eax
movzbl %al, %edx
pushq $0x7b
popq %rsi
movq -0x58(%rbp), %r12
movq %r12, %rdi
movl 0x18(%rbp), %ecx
callq 0x41f42
testq %rbx, %rbx
je 0x55265
movq %rbx, %rdi
callq 0x4d5c3
movq -0x30(%rbp), %rdi
movq %r14, %rsi
callq 0x2f476
movl -0x34(%rbp), %edx
testl %edx, %edx
jle 0x55290
movl 0x34(%r12), %eax
cmpl %edx, %eax
jbe 0x55290
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