// File: /workspace/github_c_makefile_star2_O2/skeeto[P]flappy/sqlite3.c
// Function: sqlite3WhereEnd
SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
  Parse *pParse = pWInfo->pParse;
  Vdbe *v = pParse->pVdbe;
  int i;
  WhereLevel *pLevel;
  WhereLoop *pLoop;
  SrcList *pTabList = pWInfo->pTabList;
  sqlite3 *db = pParse->db;

  /* Generate loop termination code.
  */
  VdbeModuleComment((v, "End WHERE-core"));
  sqlite3ExprCacheClear(pParse);
  for(i=pWInfo->nLevel-1; i>=0; i--){
    int addr;
    pLevel = &pWInfo->a[i];
    pLoop = pLevel->pWLoop;
    sqlite3VdbeResolveLabel(v, pLevel->addrCont);
    if( pLevel->op!=OP_Noop ){
      sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2);
      sqlite3VdbeChangeP5(v, pLevel->p5);
    }
    if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
      struct InLoop *pIn;
      int j;
      sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
      for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
        sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
        sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
        sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
      }
      sqlite3DbFree(db, pLevel->u.in.aInLoop);
    }
    sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
    if( pLevel->addrSkip ){
      sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrSkip);
      VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
      sqlite3VdbeJumpHere(v, pLevel->addrSkip);
      sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
    }
    if( pLevel->iLeftJoin ){
      addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin);
      assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
           || (pLoop->wsFlags & WHERE_INDEXED)!=0 );
      if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){
        sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
      }
      if( pLoop->wsFlags & WHERE_INDEXED ){
        sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
      }
      if( pLevel->op==OP_Return ){
        sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
      }else{
        sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
      }
      sqlite3VdbeJumpHere(v, addr);
    }
    VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
                     pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
  }

  /* The "break" point is here, just past the end of the outer loop.
  ** Set it.
  */
  sqlite3VdbeResolveLabel(v, pWInfo->iBreak);

  assert( pWInfo->nLevel<=pTabList->nSrc );
  for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
    Index *pIdx = 0;
    struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
    Table *pTab = pTabItem->pTab;
    assert( pTab!=0 );
    pLoop = pLevel->pWLoop;

    /* Close all of the cursors that were opened by sqlite3WhereBegin.
    ** Except, do not close cursors that will be reused by the OR optimization
    ** (WHERE_OMIT_OPEN_CLOSE).  And do not close the OP_OpenWrite cursors
    ** created for the ONEPASS optimization.
    */
    if( (pTab->tabFlags & TF_Ephemeral)==0
     && pTab->pSelect==0
     && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
    ){
      int ws = pLoop->wsFlags;
      if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
        sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
      }
      if( (ws & WHERE_INDEXED)!=0
       && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0 
       && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1]
      ){
        sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
      }
    }

    /* If this scan uses an index, make VDBE code substitutions to read data
    ** from the index instead of from the table where possible.  In some cases
    ** this optimization prevents the table from ever being read, which can
    ** yield a significant performance boost.
    ** 
    ** Calls to the code generator in between sqlite3WhereBegin and
    ** sqlite3WhereEnd will have created code that references the table
    ** directly.  This loop scans all that code looking for opcodes
    ** that reference the table and converts them into opcodes that
    ** reference the index.
    */
    if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
      pIdx = pLoop->u.btree.pIndex;
    }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
      pIdx = pLevel->u.pCovidx;
    }
    if( pIdx && !db->mallocFailed ){
      int k, last;
      VdbeOp *pOp;

      last = sqlite3VdbeCurrentAddr(v);
      k = pLevel->addrBody;
      pOp = sqlite3VdbeGetOp(v, k);
      for(; k<last; k++, pOp++){
        if( pOp->p1!=pLevel->iTabCur ) continue;
        if( pOp->opcode==OP_Column ){
          int x = pOp->p2;
          assert( pIdx->pTable==pTab );
          if( !HasRowid(pTab) ){
            Index *pPk = sqlite3PrimaryKeyIndex(pTab);
            x = pPk->aiColumn[x];
          }
          x = sqlite3ColumnOfIndex(pIdx, x);
          if( x>=0 ){
            pOp->p2 = x;
            pOp->p1 = pLevel->iIdxCur;
          }
          assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 );
        }else if( pOp->opcode==OP_Rowid ){
          pOp->p1 = pLevel->iIdxCur;
          pOp->opcode = OP_IdxRowid;
        }
      }
    }
  }

  /* Final cleanup
  */
  pParse->nQueryLoop = pWInfo->savedNQueryLoop;
  whereInfoFree(db, pWInfo);
  return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
movq 0x8(%rbx), %r14
movq (%rdi), %rax
movq %rax, (%rsp)
movq 0x10(%rdi), %r12
movq %rdi, 0x28(%rsp)
callq 0x50040
movzbl 0x38(%rbx), %r13d
movq %rbx, 0x18(%rsp)
addq $0x340, %rbx            # imm = 0x340
addq $0x8, %r14
movq %r14, 0x8(%rsp)
testq %r13, %r13
jle 0x4fdce
decq %r13
imulq $0x50, %r13, %rbp
movq 0x40(%rbx,%rbp), %r15
movl 0x18(%rbx,%rbp), %eax
testl %eax, %eax
jns 0x4fbbd
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x4fbbd
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
addq %rbx, %rbp
movzbl 0x25(%rbp), %esi
cmpl $0x97, %esi
je 0x4fbf4
movl 0x28(%rbp), %edx
movl 0x2c(%rbp), %ecx
movq %r12, %rdi
callq 0x43f64
movq 0x8(%r12), %rax
testq %rax, %rax
je 0x4fbf4
movb 0x26(%rbp), %cl
movslq 0x34(%r12), %rdx
imulq $0x18, %rdx, %rdx
movb %cl, -0x15(%rax,%rdx)
movq %r15, 0x10(%rsp)
testb $0x8, 0x29(%r15)
je 0x4fcb6
movl 0x30(%rbp), %r15d
testl %r15d, %r15d
jle 0x4fcb6
movl 0x10(%rbp), %eax
testl %eax, %eax
jns 0x4fc32
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x4fc32
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
movl 0x30(%rbp), %r15d
movq 0x38(%rbp), %rax
movslq %r15d, %rcx
imulq $0xc, %rcx, %rcx
leaq (%rax,%rcx), %r14
addq $-0x4, %r14
testl %r15d, %r15d
jle 0x4fca9
movl -0x4(%r14), %ecx
cmpl $-0x1, %ecx
jl 0x4fc6e
leal 0x1(%rcx), %edx
movl 0x34(%r12), %eax
cmpl %edx, %eax
jbe 0x4fc6e
movq 0x8(%r12), %rsi
movl %edx, %edx
imulq $0x18, %rdx, %rdx
movl %eax, 0x8(%rsi,%rdx)
movzbl (%r14), %esi
movl -0x8(%r14), %edx
movq %r12, %rdi
callq 0x43f64
movl -0x4(%r14), %eax
testl %eax, %eax
jle 0x4fca0
decl %eax
movl 0x34(%r12), %ecx
cmpl %eax, %ecx
jbe 0x4fca0
movq 0x8(%r12), %rdx
movl %eax, %eax
imulq $0x18, %rax, %rax
movl %ecx, 0x8(%rdx,%rax)
decl %r15d
addq $-0xc, %r14
jmp 0x4fc45
movq 0x38(%rbp), %rsi
movq (%rsp), %rdi
callq 0x9805
movl 0xc(%rbp), %eax
testl %eax, %eax
jns 0x4fcd3
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x4fcd3
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
movl 0x14(%rbp), %ecx
testl %ecx, %ecx
je 0x4fd26
movq %r12, %rdi
pushq $0x10
popq %rsi
xorl %edx, %edx
callq 0x43f64
movslq 0x14(%rbp), %rax
testq %rax, %rax
js 0x4fd26
movl 0x34(%r12), %ecx
cmpl %eax, %ecx
jbe 0x4fd06
movq 0x8(%r12), %rdx
imulq $0x18, %rax, %rsi
movl %ecx, 0x8(%rdx,%rsi)
cmpl $0x2, %eax
jb 0x4fd26
addl $-0x2, %eax
movl 0x34(%r12), %ecx
cmpl %eax, %ecx
jbe 0x4fd26
movq 0x8(%r12), %rdx
movl %eax, %eax
imulq $0x18, %rax, %rax
movl %ecx, 0x8(%rdx,%rax)
movl (%rbp), %edx
testl %edx, %edx
je 0x4fb8a
movq %r12, %rdi
movl $0x80, %esi
callq 0x457e7
movl %eax, %r15d
movq 0x10(%rsp), %r14
movl 0x28(%r14), %eax
testb $0x40, %al
jne 0x4fd6a
imulq $0x68, %r13, %rax
movq 0x8(%rsp), %rcx
movl 0x3c(%rcx,%rax), %edx
movq %r12, %rdi
pushq $0x63
popq %rsi
callq 0x457e7
movl 0x28(%r14), %eax
btl $0x9, %eax
jae 0x4fd7e
movl 0x8(%rbp), %edx
movq %r12, %rdi
pushq $0x63
popq %rsi
callq 0x457e7
cmpb $0x12, 0x25(%rbp)
jne 0x4fd92
movl 0x1c(%rbp), %ecx
movl 0x28(%rbp), %edx
movq %r12, %rdi
pushq $0x11
popq %rsi
jmp 0x4fd9d
movl 0x1c(%rbp), %ecx
movq %r12, %rdi
pushq $0x10
popq %rsi
xorl %edx, %edx
callq 0x43f64
testl %r15d, %r15d
js 0x4fb8a
movl 0x34(%r12), %eax
cmpl %r15d, %eax
jbe 0x4fb8a
movq 0x8(%r12), %rcx
movl %r15d, %edx
imulq $0x18, %rdx, %rdx
movl %eax, 0x8(%rcx,%rdx)
jmp 0x4fb8a
movq 0x18(%rsp), %r13
movl 0x44(%r13), %eax
testl %eax, %eax
jns 0x4fdf1
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x4fdf1
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
xorl %ecx, %ecx
movzbl 0x38(%r13), %eax
cmpl %eax, %ecx
jae 0x4ff5a
movl %ecx, 0x10(%rsp)
movzbl 0x24(%rbx), %eax
imulq $0x68, %rax, %rax
movq 0x8(%rsp), %rcx
movq 0x20(%rcx,%rax), %rsi
movq 0x40(%rbx), %r14
testb $0x2, 0x48(%rsi)
jne 0x4fe81
cmpq $0x0, 0x18(%rsi)
jne 0x4fe81
testb $0x10, 0x32(%r13)
jne 0x4fe81
movl 0x28(%r14), %ebp
cmpb $0x0, 0x35(%r13)
jne 0x4fe59
movl %ebp, %ecx
andl $0x40, %ecx
jne 0x4fe59
addq 0x8(%rsp), %rax
movl 0x3c(%rax), %edx
movq %rsi, %r15
movq %r12, %rdi
pushq $0x38
popq %rsi
callq 0x457e7
movq %r15, %rsi
andl $0x4300, %ebp           # imm = 0x4300
cmpl $0x200, %ebp            # imm = 0x200
jne 0x4fe81
movl 0x8(%rbx), %edx
cmpl 0x50(%r13), %edx
je 0x4fe81
movq %rsi, %r15
movq %r12, %rdi
pushq $0x38
popq %rsi
callq 0x457e7
movq %r15, %rsi
movl 0x28(%r14), %eax
testw $0x240, %ax             # imm = 0x240
je 0x4fe91
addq $0x20, %r14
jmp 0x4fe9b
btl $0xd, %eax
jae 0x4feb2
leaq 0x30(%rbx), %r14
movq (%r14), %rax
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x4feb2
movq (%rsp), %rax
cmpb $0x0, 0x50(%rax)
je 0x4fec1
movl 0x10(%rsp), %ecx
incl %ecx
addq $0x50, %rbx
jmp 0x4fdf3
movl 0x34(%r12), %r14d
movl 0x20(%rbx), %ebp
movq %rsi, 0x20(%rsp)
movq %r12, %rdi
movl %ebp, %esi
callq 0x48744
movq 0x20(%rsp), %rdi
movq %rax, %r15
addq $0x8, %r15
cmpl %r14d, %ebp
jge 0x4feb2
movl -0x4(%r15), %eax
cmpl 0x4(%rbx), %eax
jne 0x4ff52
movzbl -0x8(%r15), %eax
cmpl $0x62, %eax
je 0x4ff46
cmpl $0x2b, %eax
jne 0x4ff52
movl (%r15), %eax
testb $0x20, 0x48(%rdi)
je 0x4ff20
movslq %eax, %r13
callq 0x30033
movq 0x8(%rax), %rax
movzwl (%rax,%r13,2), %eax
movq 0x18(%rsp), %r13
movswl %ax, %esi
movq 0x30(%rsp), %rdi
callq 0x467ab
testw %ax, %ax
movq 0x20(%rsp), %rdi
js 0x4ff52
movzwl %ax, %eax
movl %eax, (%r15)
movl 0x8(%rbx), %eax
movl %eax, -0x4(%r15)
jmp 0x4ff52
movl 0x8(%rbx), %eax
movl %eax, -0x4(%r15)
movb $0x6b, -0x8(%r15)
incl %ebp
addq $0x18, %r15
jmp 0x4fee4
movl 0x48(%r13), %eax
movq 0x28(%rsp), %rcx
movl %eax, 0x1f8(%rcx)
movq (%rsp), %rdi
movq %r13, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x518e3