// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x800cf
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
movw 0x14(%rax), %cx
movq -0x18(%rbp), %rax
movswl %cx, %edx
movswl 0x16(%rax), %ecx
callq 0x80330
movl $0x0, -0x4(%rbp)
jmp 0x8031b
movq -0x38(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x80275
movq -0x28(%rbp), %rax
movzbl 0x10(%rax), %eax
movq -0x18(%rbp), %rcx
movzbl 0x10(%rcx), %ecx
cmpl %ecx, %eax
jne 0x80119
movq -0x28(%rbp), %rax
movzbl 0x11(%rax), %eax
movq -0x18(%rbp), %rcx
movzbl 0x11(%rcx), %ecx
cmpl %ecx, %eax
je 0x8011e
jmp 0x80259
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
andq (%rcx), %rax
movq -0x28(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x8020a
movq -0x28(%rbp), %rax
movswl 0x12(%rax), %eax
movq -0x18(%rbp), %rcx
movswl 0x12(%rcx), %ecx
cmpl %ecx, %eax
jg 0x8020a
movq -0x28(%rbp), %rax
movswl 0x14(%rax), %eax
movq -0x18(%rbp), %rcx
movswl 0x14(%rcx), %ecx
cmpl %ecx, %eax
jg 0x8020a
movq -0x28(%rbp), %rax
movswl 0x16(%rax), %eax
movq -0x18(%rbp), %rcx
movswl 0x16(%rcx), %ecx
cmpl %ecx, %eax
jg 0x8020a
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x80205
movq -0x28(%rbp), %rax
movzwl 0x2c(%rax), %eax
movq -0x18(%rbp), %rcx
movzwl 0x2c(%rcx), %ecx
cmpl %ecx, %eax
jge 0x80205
movq -0x28(%rbp), %rax
movl 0x28(%rax), %eax
movq -0x18(%rbp), %rcx
andl 0x28(%rcx), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0x80205
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
cmpq 0x20(%rcx), %rax
je 0x801f7
movq -0x18(%rbp), %rax
movswl 0x14(%rax), %eax
movq -0x28(%rbp), %rcx
movzwl 0x2c(%rcx), %ecx
addl %ecx, %eax
movq -0x28(%rbp), %rcx
movswl 0x14(%rcx), %ecx
movq -0x18(%rbp), %rdx
movzwl 0x2c(%rdx), %edx
addl %edx, %ecx
cmpl %ecx, %eax
jg 0x80205
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x80275
jmp 0x80314
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
andq (%rcx), %rax
movq -0x18(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x80257
movq -0x28(%rbp), %rax
movswl 0x14(%rax), %eax
movq -0x18(%rbp), %rcx
movswl 0x14(%rcx), %ecx
cmpl %ecx, %eax
jl 0x80257
movq -0x28(%rbp), %rax
movswl 0x16(%rax), %eax
movq -0x18(%rbp), %rcx
movswl 0x16(%rcx), %ecx
cmpl %ecx, %eax
jl 0x80257
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x80275
jmp 0x80259
movq -0x28(%rbp), %rax
addq $0x38, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x800e6
cmpq $0x0, -0x28(%rbp)
jne 0x802a7
movq -0x40(%rbp), %rdi
movl $0x60, %esi
callq 0x16700
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x8029e
movl $0x7, -0x4(%rbp)
jmp 0x8031b
movq -0x28(%rbp), %rdi
callq 0x78930
movq -0x40(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x804f0
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movl 0x28(%rax), %eax
andl $0x400, %eax            # imm = 0x400
cmpl $0x0, %eax
jne 0x8030b
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x80309
movq -0x48(%rbp), %rax
cmpl $0x0, 0x58(%rax)
jne 0x80309
movq -0x28(%rbp), %rax
movq $0x0, 0x20(%rax)
jmp 0x8030b
movl $0x0, -0x4(%rbp)
jmp 0x8031b
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)