// File: /workspace/github_c_makefile_star2_O2/skeeto[P]flappy/sqlite3.c
// Function: whereLoopInsert
static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
  WhereLoop **ppPrev, *p, *pNext = 0;
  WhereInfo *pWInfo = pBuilder->pWInfo;
  sqlite3 *db = pWInfo->pParse->db;

  /* If pBuilder->pOrSet is defined, then only keep track of the costs
  ** and prereqs.
  */
  if( pBuilder->pOrSet!=0 ){
#if WHERETRACE_ENABLED
    u16 n = pBuilder->pOrSet->n;
    int x =
#endif
    whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
                                    pTemplate->nOut);
#if WHERETRACE_ENABLED /* 0x8 */
    if( sqlite3WhereTrace & 0x8 ){
      sqlite3DebugPrintf(x?"   or-%d:  ":"   or-X:  ", n);
      whereLoopPrint(pTemplate, pBuilder->pWC);
    }
#endif
    return SQLITE_OK;
  }

  /* Search for an existing WhereLoop to overwrite, or which takes
  ** priority over pTemplate.
  */
  for(ppPrev=&pWInfo->pLoops, p=*ppPrev; p; ppPrev=&p->pNextLoop, p=*ppPrev){
    if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
      /* If either the iTab or iSortIdx values for two WhereLoop are different
      ** then those WhereLoops need to be considered separately.  Neither is
      ** a candidate to replace the other. */
      continue;
    }
    /* In the current implementation, the rSetup value is either zero
    ** or the cost of building an automatic index (NlogN) and the NlogN
    ** is the same for compatible WhereLoops. */
    assert( p->rSetup==0 || pTemplate->rSetup==0 
                 || p->rSetup==pTemplate->rSetup );

    /* whereLoopAddBtree() always generates and inserts the automatic index
    ** case first.  Hence compatible candidate WhereLoops never have a larger
    ** rSetup. Call this SETUP-INVARIANT */
    assert( p->rSetup>=pTemplate->rSetup );

    if( (p->prereq & pTemplate->prereq)==p->prereq
     && p->rSetup<=pTemplate->rSetup
     && p->rRun<=pTemplate->rRun
     && p->nOut<=pTemplate->nOut
    ){
      /* This branch taken when p is equal or better than pTemplate in 
      ** all of (1) dependencies (2) setup-cost, (3) run-cost, and
      ** (4) number of output rows. */
      assert( p->rSetup==pTemplate->rSetup );
      if( p->prereq==pTemplate->prereq
       && p->nLTerm<pTemplate->nLTerm
       && (p->wsFlags & pTemplate->wsFlags & WHERE_INDEXED)!=0
       && (p->u.btree.pIndex==pTemplate->u.btree.pIndex
          || pTemplate->rRun+p->nLTerm<=p->rRun+pTemplate->nLTerm)
      ){
        /* Overwrite an existing WhereLoop with an similar one that uses
        ** more terms of the index */
        pNext = p->pNextLoop;
        break;
      }else{
        /* pTemplate is not helpful.
        ** Return without changing or adding anything */
        goto whereLoopInsert_noop;
      }
    }
    if( (p->prereq & pTemplate->prereq)==pTemplate->prereq
     && p->rRun>=pTemplate->rRun
     && p->nOut>=pTemplate->nOut
    ){
      /* Overwrite an existing WhereLoop with a better one: one that is
      ** better at one of (1) dependencies, (2) setup-cost, (3) run-cost
      ** or (4) number of output rows, and is no worse in any of those
      ** categories. */
      assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
      pNext = p->pNextLoop;
      break;
    }
  }

  /* If we reach this point it means that either p[] should be overwritten
  ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
  ** WhereLoop and insert it.
  */
#if WHERETRACE_ENABLED /* 0x8 */
  if( sqlite3WhereTrace & 0x8 ){
    if( p!=0 ){
      sqlite3DebugPrintf("ins-del:  ");
      whereLoopPrint(p, pBuilder->pWC);
    }
    sqlite3DebugPrintf("ins-new:  ");
    whereLoopPrint(pTemplate, pBuilder->pWC);
  }
#endif
  if( p==0 ){
    p = sqlite3DbMallocRaw(db, sizeof(WhereLoop));
    if( p==0 ) return SQLITE_NOMEM;
    whereLoopInit(p);
  }
  whereLoopXfer(db, p, pTemplate);
  p->pNextLoop = pNext;
  *ppPrev = p;
  if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
    Index *pIndex = p->u.btree.pIndex;
    if( pIndex && pIndex->tnum==0 ){
      p->u.btree.pIndex = 0;
    }
  }
  return SQLITE_OK;

  /* Jump here if the insert is a no-op */
whereLoopInsert_noop:
#if WHERETRACE_ENABLED /* 0x8 */
  if( sqlite3WhereTrace & 0x8 ){
    sqlite3DebugPrintf("ins-noop: ");
    whereLoopPrint(pTemplate, pBuilder->pWC);
  }
#endif
  return SQLITE_OK;  
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rsi, %rbx
movq %rdi, %rax
movq 0x20(%rdi), %rdi
testq %rdi, %rdi
je 0x539d4
movq (%rbx), %rsi
movswl 0x14(%rbx), %edx
movswl 0x16(%rbx), %ecx
callq 0x53b88
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq (%rax), %r12
movq (%r12), %rax
movq (%rax), %r15
addq $0x20, %r12
movq (%r12), %r14
testq %r14, %r14
je 0x53a5f
movb 0x10(%r14), %al
cmpb 0x10(%rbx), %al
jne 0x53a56
movb 0x11(%r14), %al
cmpb 0x11(%rbx), %al
jne 0x53a56
movq (%r14), %rdi
movq (%rbx), %rsi
movq %rsi, %rax
andq %rdi, %rax
cmpq %rdi, %rax
jne 0x53a37
movzwl 0x12(%r14), %ecx
cmpw 0x12(%rbx), %cx
jg 0x53a37
movswl 0x14(%r14), %ecx
movswl 0x14(%rbx), %edx
cmpw %dx, %cx
jg 0x53a37
movzwl 0x16(%r14), %r8d
cmpw 0x16(%rbx), %r8w
jle 0x53b39
cmpq %rsi, %rax
jne 0x53a56
movzwl 0x14(%r14), %eax
cmpw 0x14(%rbx), %ax
jl 0x53a56
movzwl 0x16(%r14), %eax
cmpw 0x16(%rbx), %ax
jge 0x53b7f
addq $0x38, %r14
movq %r14, %r12
jmp 0x539e2
pushq $0x60
popq %rsi
movq %r15, %rdi
callq 0x10a37
testq %rax, %rax
je 0x53ab3
movq %rax, %r14
addq $0x40, %rax
movq %rax, 0x30(%r14)
movabsq $0x4000000000000, %rax  # imm = 0x4000000000000
movq %rax, 0x28(%r14)
xorl %r13d, %r13d
movq %r15, %rdi
movq %r14, %rsi
callq 0x53c35
movzwl 0x2c(%rbx), %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x53924
testl %eax, %eax
je 0x53abb
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r14)
jmp 0x53b03
pushq $0x7
popq %rax
jmp 0x539ca
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x1e(%rbx), %xmm2
movups %xmm2, 0x1e(%r14)
movups %xmm1, 0x10(%r14)
movups %xmm0, (%r14)
movq 0x30(%r14), %rdi
movq 0x30(%rbx), %rsi
movzwl 0x2c(%r14), %edx
shll $0x3, %edx
callq 0x7240
movl 0x28(%rbx), %eax
btl $0xa, %eax
jb 0x53aff
btl $0xe, %eax
jae 0x53b03
andq $0x0, 0x20(%rbx)
jmp 0x53b03
movb $0x0, 0x1c(%rbx)
movq %r13, 0x38(%r14)
movq %r14, (%r12)
xorl %eax, %eax
testb $0x4, 0x29(%r14)
jne 0x539ca
movq 0x20(%r14), %rcx
testq %rcx, %rcx
je 0x539ca
cmpl $0x0, 0x58(%rcx)
jne 0x539ca
andq $0x0, 0x20(%r14)
jmp 0x539ca
xorl %eax, %eax
cmpq %rsi, %rdi
jne 0x539ca
movzwl 0x2c(%r14), %esi
movzwl 0x2c(%rbx), %edi
cmpw %di, %si
jae 0x539ca
movl 0x28(%r14), %r8d
andl 0x28(%rbx), %r8d
btl $0x9, %r8d
jae 0x539ca
movq 0x20(%r14), %r8
cmpq 0x20(%rbx), %r8
je 0x53b7f
addl %edx, %esi
addl %ecx, %edi
cmpl %edi, %esi
jg 0x539ca
movq 0x38(%r14), %r13
jmp 0x53a8b