// File: /workspace/github_c_makefile_star2_O1/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 $0xa8, %rsp
movq %r9, -0x78(%rbp)
movq %r8, %r15
movq %rcx, -0x80(%rbp)
movq %rdx, -0x70(%rbp)
movq %rsi, -0x68(%rbp)
movq %rdi, %r14
movq (%rdi), %r13
callq 0x45b57
movq %rax, %rbx
xorl %eax, %eax
cmpl $0x0, 0x18(%rbp)
jns 0x5e8bf
movzbl 0x2c(%r15), %edx
movq %rbx, %rdi
movl $0x7c, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movq %r14, -0x40(%rbp)
movl %eax, -0x2c(%rbp)
movq %rbx, -0x58(%rbp)
movl 0x10(%rbp), %eax
cmpl $0x0, 0x28(%r15)
movq %r15, -0x48(%rbp)
movq %r13, -0x50(%rbp)
jle 0x5ea21
incl %eax
movl %eax, -0x5c(%rbp)
leaq 0x40(%r15), %rax
movq %rax, -0x88(%rbp)
xorl %ebx, %ebx
movq -0x78(%rbp), %r12
movq $0x0, -0x38(%rbp)
movq -0x48(%rbp), %r14
movq $0x0, -0xc0(%rbp)
movl $0x0, -0xb8(%rbp)
movq %r13, %rdi
movl $0x84, %esi
leaq -0xc0(%rbp), %rdx
xorl %ecx, %ecx
callq 0x4a949
testq %rax, %rax
je 0x5e999
movq %rax, %r15
movq -0x80(%rbp), %rax
testq %rax, %rax
je 0x5e99e
movq 0x8(%rax), %rax
movslq (%rax,%rbx,4), %rax
movq -0x70(%rbp), %rsi
movq 0x8(%rsi), %rcx
leaq (%rax,%rax,2), %rdx
shlq $0x4, %rdx
movswl 0x40(%rsi), %esi
cmpl %esi, %eax
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmovel %esi, %eax
addl -0x5c(%rbp), %eax
movl %eax, 0x2c(%r15)
movb 0x29(%rcx,%rdx), %al
movb %al, 0x1(%r15)
movq 0x20(%rcx,%rdx), %rdx
testq %rdx, %rdx
jne 0x5e988
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rdx
movq -0x40(%rbp), %rdi
movq %r15, %rsi
callq 0x48127
movq %rax, %r15
jmp 0x5e9aa
xorl %r15d, %r15d
jmp 0x5e9aa
movl 0x10(%rbp), %eax
movl %eax, 0x2c(%r15)
movb $0x64, 0x1(%r15)
cmpq $0x0, -0x78(%rbp)
movq %r12, %rax
cmoveq -0x88(%rbp), %rax
movslq (%rax), %rax
movq (%r14), %rcx
movq 0x8(%rcx), %rcx
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq (%rcx,%rax), %rdx
movq -0x50(%rbp), %r13
movq %r13, %rdi
movl $0x1a, %esi
callq 0x3d5ef
movq -0x40(%rbp), %rdi
movl $0x4c, %esi
movq %r15, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x39f77
movq %r13, %rdi
movq -0x38(%rbp), %rsi
movq %rax, %rdx
callq 0x4a740
movq %rax, -0x38(%rbp)
incq %rbx
movslq 0x28(%r14), %rax
addq $0x4, %r12
cmpq %rax, %rbx
jl 0x5e902
jmp 0x5ea29
movq $0x0, -0x38(%rbp)
movl 0x18(%rbp), %r15d
testl %r15d, %r15d
jle 0x5eaf3
movq -0x48(%rbp), %rax
movq -0x70(%rbp), %rcx
cmpq %rcx, (%rax)
jne 0x5eaf3
xorl %r12d, %r12d
leaq -0xc0(%rbp), %r13
movq %r12, (%r13)
movl %r12d, 0x8(%r13)
movq -0x50(%rbp), %r14
movq %r14, %rdi
movl $0x84, %esi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x4a949
movq %rax, %rbx
movq %r12, (%r13)
movl %r12d, 0x8(%r13)
leaq -0xc0(%rbp), %rdx
movq %r14, %rdi
movl $0x98, %esi
xorl %ecx, %ecx
callq 0x4a949
testq %rbx, %rbx
setne %cl
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x5eabd
movl 0x10(%rbp), %ecx
movl %ecx, 0x2c(%rbx)
movb $0x64, 0x1(%rbx)
movq -0x68(%rbp), %rcx
movl 0x44(%rcx), %ecx
movl %ecx, 0x2c(%rax)
movw $0xffff, 0x30(%rax)     # imm = 0xFFFF
movq -0x40(%rbp), %r15
movq %r15, %rdi
movl $0x4b, %esi
movq %rbx, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x39f77
movq %r14, %rdi
movq -0x38(%rbp), %rsi
movq %rax, %rdx
callq 0x4a740
movq %rax, %r14
movq -0x58(%rbp), %r12
movl -0x2c(%rbp), %r13d
jmp 0x5eb03
movq -0x58(%rbp), %r12
movl -0x2c(%rbp), %r13d
movq -0x40(%rbp), %r15
movq -0x38(%rbp), %r14
xorps %xmm0, %xmm0
leaq -0xc0(%rbp), %rdi
movaps %xmm0, (%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movq $0x0, 0x30(%rdi)
movq -0x68(%rbp), %rbx
movq %rbx, 0x8(%rdi)
movq %r15, (%rdi)
movq %r14, %rsi
callq 0x467fe
movl $0x0, (%rsp)
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x51e2a
movq %rax, %rbx
movl 0x18(%rbp), %ecx
testl %ecx, %ecx
jle 0x5eb76
movq -0x48(%rbp), %rax
cmpb $0x0, 0x2c(%rax)
jne 0x5eb76
movq 0x1d8(%r15), %rax
testq %rax, %rax
cmoveq %r15, %rax
movb $0x1, 0x24(%rax)
movq -0x48(%rbp), %rax
movzbl 0x2c(%rax), %edx
movq %r12, %rdi
movl $0x7b, %esi
xorl %r8d, %r8d
callq 0x45b9e
testq %rbx, %rbx
je 0x5eb9b
movq %rbx, %rdi
callq 0x54d54
movq -0x50(%rbp), %rdi
movq %r14, %rsi
callq 0x34068
testl %r13d, %r13d
jle 0x5ebc6
movl 0x34(%r12), %eax
cmpl %r13d, %eax
jbe 0x5ebc6
movq 0x8(%r12), %rcx
movl %r13d, %edx
leaq (%rdx,%rdx,2), %rdx
movl %eax, 0x8(%rcx,%rdx,8)
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq