// File: /workspace/github_c_makefile_star2_O3/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, -0x68(%rbp)
movq %r8, %r15
movq %rcx, -0x78(%rbp)
movq %rdx, -0x60(%rbp)
movq %rsi, -0x58(%rbp)
movq %rdi, %r14
movq (%rdi), %rax
movq %rax, -0x40(%rbp)
callq 0x45ece
movq %rax, %rbx
xorl %eax, %eax
cmpl $0x0, 0x18(%rbp)
jns 0x5e754
movzbl 0x2c(%r15), %edx
movq %rbx, %rdi
movl $0x7c, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45f15
movq %r14, -0x30(%rbp)
movl %eax, -0x4c(%rbp)
movq %rbx, -0x70(%rbp)
movl 0x10(%rbp), %eax
cmpl $0x0, 0x28(%r15)
movq %r15, -0x48(%rbp)
jle 0x5e8ac
incl %eax
movl %eax, -0x50(%rbp)
leaq 0x40(%r15), %rax
movq %rax, -0x80(%rbp)
xorl %ebx, %ebx
movq -0x68(%rbp), %r12
movq $0x0, -0x38(%rbp)
movq -0x40(%rbp), %r14
movq -0x48(%rbp), %r15
movq $0x0, -0xb8(%rbp)
movl $0x0, -0xb0(%rbp)
movq %r14, %rdi
movl $0x84, %esi
leaq -0xb8(%rbp), %rdx
xorl %ecx, %ecx
callq 0x4aaef
testq %rax, %rax
je 0x5e827
movq %rax, %r13
movq -0x78(%rbp), %rax
testq %rax, %rax
je 0x5e82c
movq 0x8(%rax), %rax
movslq (%rax,%rbx,4), %rax
movq -0x60(%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 -0x50(%rbp), %eax
movl %eax, 0x2c(%r13)
movb 0x29(%rcx,%rdx), %al
movb %al, 0x1(%r13)
movq 0x20(%rcx,%rdx), %rdx
testq %rdx, %rdx
jne 0x5e816
movq 0x10(%r14), %rax
movq (%rax), %rdx
movq -0x30(%rbp), %rdi
movq %r13, %rsi
callq 0x48450
movq %rax, %r13
jmp 0x5e838
xorl %r13d, %r13d
jmp 0x5e838
movl 0x10(%rbp), %eax
movl %eax, 0x2c(%r13)
movb $0x64, 0x1(%r13)
cmpq $0x0, -0x68(%rbp)
movq %r12, %rax
cmoveq -0x80(%rbp), %rax
movslq (%rax), %rax
movq (%r15), %rcx
movq 0x8(%rcx), %rcx
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq (%rcx,%rax), %rdx
movq -0x40(%rbp), %r14
movq %r14, %rdi
movl $0x1a, %esi
callq 0x3db22
movq -0x30(%rbp), %rdi
movl $0x4c, %esi
movq %r13, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x3a637
movq %r14, %rdi
movq -0x38(%rbp), %rsi
movq %rax, %rdx
callq 0x4a8e5
movq %rax, -0x38(%rbp)
incq %rbx
movslq 0x28(%r15), %rax
addq $0x4, %r12
cmpq %rax, %rbx
jl 0x5e794
jmp 0x5e8b8
movq $0x0, -0x38(%rbp)
movq -0x40(%rbp), %r14
cmpl $0x0, 0x18(%rbp)
jle 0x5e97c
movq -0x48(%rbp), %rax
movq -0x60(%rbp), %rcx
cmpq %rcx, (%rax)
movq -0x38(%rbp), %r15
jne 0x5e976
xorl %r12d, %r12d
leaq -0xb8(%rbp), %r13
movq %r12, (%r13)
movl %r12d, 0x8(%r13)
movq %r14, %rdi
movl $0x84, %esi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x4aaef
movq %rax, %rbx
movq %r12, (%r13)
movl %r12d, 0x8(%r13)
leaq -0xb8(%rbp), %rdx
movq %r14, %rdi
movl $0x98, %esi
xorl %ecx, %ecx
callq 0x4aaef
testq %rbx, %rbx
setne %cl
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x5e949
movl 0x10(%rbp), %ecx
movl %ecx, 0x2c(%rbx)
movb $0x64, 0x1(%rbx)
movq -0x58(%rbp), %rcx
movl 0x44(%rcx), %ecx
movl %ecx, 0x2c(%rax)
movw $0xffff, 0x30(%rax)     # imm = 0xFFFF
movq -0x30(%rbp), %r13
movq %r13, %rdi
movl $0x4b, %esi
movq %rbx, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x3a637
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x4a8e5
movq %rax, %r15
jmp 0x5e984
movq -0x30(%rbp), %r13
jmp 0x5e984
movq -0x30(%rbp), %r13
movq -0x38(%rbp), %r15
cmpl $0x0, 0x18(%rbp)
setg %r12b
xorps %xmm0, %xmm0
leaq -0xb8(%rbp), %rdi
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movq $0x0, 0x30(%rdi)
movq -0x58(%rbp), %rbx
movq %rbx, 0x8(%rdi)
movq %r13, (%rdi)
movq %r15, %rsi
callq 0x46b2f
movl $0x0, (%rsp)
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x51e94
movq %rax, %rbx
movq -0x48(%rbp), %rax
movb 0x2c(%rax), %al
testb %al, %al
sete %cl
andb %r12b, %cl
cmpb $0x1, %cl
jne 0x5ea01
movq 0x1d8(%r13), %rax
testq %rax, %rax
cmoveq %r13, %rax
movb $0x1, 0x24(%rax)
xorl %eax, %eax
movzbl %al, %edx
movq -0x70(%rbp), %r12
movq %r12, %rdi
movl $0x7b, %esi
movl 0x18(%rbp), %ecx
xorl %r8d, %r8d
callq 0x45f15
testq %rbx, %rbx
je 0x5ea28
movq %rbx, %rdi
callq 0x54d64
movq %r14, %rdi
movq %r15, %rsi
callq 0x3479d
movl -0x4c(%rbp), %edx
testl %edx, %edx
jle 0x5ea52
movl 0x34(%r12), %eax
cmpl %edx, %eax
jbe 0x5ea52
movq 0x8(%r12), %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