// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/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
    if( sqlite3WhereTrace & 0x8 ){
      sqlite3DebugPrintf(x?"   or-%d:  ":"   or-X:  ", n);
      whereLoopPrint(pTemplate, pWInfo->pTabList);
    }
#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
    ){
      /* This branch taken when p is equal or better than pTemplate in 
      ** all of (1) dependences (2) setup-cost, and (3) run-cost. */
      assert( p->rSetup==pTemplate->rSetup );
      if( p->nLTerm<pTemplate->nLTerm
       && (p->wsFlags & WHERE_INDEXED)!=0
       && (pTemplate->wsFlags & WHERE_INDEXED)!=0
       && p->u.btree.pIndex==pTemplate->u.btree.pIndex
       && p->prereq==pTemplate->prereq
      ){
        /* 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
     && ALWAYS(p->rSetup>=pTemplate->rSetup) /* See SETUP-INVARIANT above */
    ){
      /* Overwrite an existing WhereLoop with a better one: one that is
      ** better at one of (1) dependences, (2) setup-cost, or (3) run-cost
      ** and is no worse in any of those categories. */
      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
  if( sqlite3WhereTrace & 0x8 ){
    if( p!=0 ){
      sqlite3DebugPrintf("ins-del:  ");
      whereLoopPrint(p, pWInfo->pTabList);
    }
    sqlite3DebugPrintf("ins-new:  ");
    whereLoopPrint(pTemplate, pWInfo->pTabList);
  }
#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
  if( sqlite3WhereTrace & 0x8 ){
    sqlite3DebugPrintf("ins-noop: ");
    whereLoopPrint(pTemplate, pWInfo->pTabList);
  }
#endif
  return SQLITE_OK;  
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbx
testq %rsi, %rsi
je 0x5a16e
movq (%rbx), %rax
movzwl 0x14(%rbx), %edx
movzwl 0x16(%rbx), %ecx
movq %rsi, %rdi
movq %rax, %rsi
callq 0x5a38b
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %r14
movq (%rdi), %rax
movq (%rax), %r12
movq 0x20(%rdi), %r15
addq $0x20, %r14
testq %r15, %r15
je 0x5a1f3
movb 0x10(%rbx), %al
cmpb %al, 0x10(%r15)
jne 0x5a1e0
movb 0x11(%r15), %cl
cmpb 0x11(%rbx), %cl
jne 0x5a1e0
movq (%r15), %rdx
movq (%rbx), %rcx
movq %rcx, %rsi
andq %rdx, %rsi
cmpq %rdx, %rsi
jne 0x5a1c1
movzwl 0x12(%r15), %edi
cmpw 0x12(%rbx), %di
ja 0x5a1c1
movzwl 0x14(%r15), %edi
cmpw 0x14(%rbx), %di
jbe 0x5a2d0
cmpq %rcx, %rsi
jne 0x5a1e0
movzwl 0x14(%r15), %ecx
cmpw 0x14(%rbx), %cx
jb 0x5a1e0
movzwl 0x12(%r15), %ecx
cmpw 0x12(%rbx), %cx
jae 0x5a30d
movq %r15, %r14
movq 0x38(%r15), %rcx
addq $0x38, %r14
movq %rcx, %r15
testq %rcx, %rcx
jne 0x5a187
movq %r12, %rdi
movl $0x60, %esi
callq 0x11721
testq %rax, %rax
je 0x5a28a
movq %rax, %r15
addq $0x40, %rax
movq %rax, 0x30(%r15)
movabsq $0x4000000000000, %rax  # imm = 0x4000000000000
movq %rax, 0x28(%r15)
xorl %r13d, %r13d
movzwl 0x2c(%rbx), %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x5a0a9
testl %eax, %eax
jne 0x5a298
movq %r12, %rdi
movq %r15, %rsi
callq 0x5a435
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x1e(%rbx), %xmm2
movups %xmm2, 0x1e(%r15)
movups %xmm1, 0x10(%r15)
movups %xmm0, (%r15)
movq 0x30(%r15), %rdi
movq 0x30(%rbx), %rsi
movzwl 0x2c(%r15), %edx
shll $0x3, %edx
callq 0x82f0
movl 0x28(%rbx), %eax
btl $0xa, %eax
jb 0x5a294
btl $0xe, %eax
jae 0x5a298
movq $0x0, 0x20(%rbx)
jmp 0x5a298
movl $0x7, %eax
jmp 0x5a15f
movb $0x0, 0x1c(%rbx)
movq %r13, 0x38(%r15)
movq %r15, (%r14)
xorl %eax, %eax
testb $0x4, 0x29(%r15)
jne 0x5a15f
movq 0x20(%r15), %rcx
testq %rcx, %rcx
je 0x5a15f
cmpl $0x0, 0x50(%rcx)
jne 0x5a15f
movq $0x0, 0x20(%r15)
jmp 0x5a15f
movzwl 0x2c(%r15), %esi
xorl %eax, %eax
cmpw 0x2c(%rbx), %si
jae 0x5a15f
testb $0x2, 0x29(%r15)
je 0x5a15f
testb $0x2, 0x29(%rbx)
je 0x5a15f
cmpq %rcx, %rdx
jne 0x5a15f
movq 0x20(%rbx), %rcx
cmpq %rcx, 0x20(%r15)
jne 0x5a15f
movq 0x38(%r15), %r13
jmp 0x5a225