// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
movq 0x8(%rbx), %rax
movq %rax, 0x10(%rsp)
movq (%rdi), %rax
movq %rax, 0x18(%rsp)
movq 0x10(%rdi), %r12
movq %rdi, 0x28(%rsp)
callq 0x5882a
movq %rbx, 0x8(%rsp)
movzbl 0x38(%rbx), %r14d
testq %r14, %r14
je 0x58530
movq 0x8(%rsp), %rax
leaq 0x340(%rax), %r13
movq 0x10(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x38(%rsp)
movq %r13, 0x30(%rsp)
movq %r14, 0x40(%rsp)
decq %r14
leaq (%r14,%r14,4), %rbx
shlq $0x4, %rbx
movq 0x40(%r13,%rbx), %r15
movl 0x18(%r13,%rbx), %eax
testl %eax, %eax
jns 0x582d7
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x582d7
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
addq %r13, %rbx
movzbl 0x25(%rbx), %esi
cmpl $0x97, %esi
je 0x58311
movl 0x28(%rbx), %edx
movl 0x2c(%rbx), %ecx
movq %r12, %rdi
xorl %r8d, %r8d
callq 0x487d8
movq 0x8(%r12), %rax
testq %rax, %rax
je 0x58311
movb 0x26(%rbx), %cl
movslq 0x34(%r12), %rdx
leaq (%rdx,%rdx,2), %rdx
movb %cl, -0x15(%rax,%rdx,8)
testb $0x8, 0x29(%r15)
je 0x583cb
movl 0x30(%rbx), %ebp
testl %ebp, %ebp
jle 0x583cb
movl 0x10(%rbx), %eax
testl %eax, %eax
js 0x584f9
leaq 0x38(%rbx), %rax
movq %rax, 0x20(%rsp)
movq 0x38(%rbx), %rsi
movl %ebp, %eax
leaq (%rax,%rax,2), %rax
leaq (%rsi,%rax,4), %r13
addq $-0x4, %r13
incl %ebp
movl -0x4(%r13), %ecx
cmpl $-0x1, %ecx
jl 0x58373
leal 0x1(%rcx), %edx
movl 0x34(%r12), %eax
cmpl %edx, %eax
jbe 0x58373
movq 0x8(%r12), %rsi
movl %edx, %edx
leaq (%rdx,%rdx,2), %rdx
movl %eax, 0x8(%rsi,%rdx,8)
movzbl (%r13), %esi
movl -0x8(%r13), %edx
movq %r12, %rdi
xorl %r8d, %r8d
callq 0x487d8
movl -0x4(%r13), %eax
testl %eax, %eax
jle 0x583a9
decl %eax
movl 0x34(%r12), %ecx
cmpl %eax, %ecx
jbe 0x583a9
movq 0x8(%r12), %rdx
movl %eax, %eax
leaq (%rax,%rax,2), %rax
movl %ecx, 0x8(%rdx,%rax,8)
addq $-0xc, %r13
decl %ebp
cmpl $0x1, %ebp
jg 0x5834f
movq 0x20(%rsp), %rax
movq (%rax), %rsi
movq 0x30(%rsp), %r13
movq 0x18(%rsp), %rdi
callq 0x9b42
movl 0xc(%rbx), %eax
testl %eax, %eax
jns 0x583e8
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x583e8
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
movl 0x14(%rbx), %ecx
testl %ecx, %ecx
je 0x58440
movq %r12, %rdi
movl $0x10, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x487d8
movslq 0x14(%rbx), %rax
testq %rax, %rax
js 0x58440
movl 0x34(%r12), %ecx
cmpl %eax, %ecx
jbe 0x58420
movq 0x8(%r12), %rdx
leaq (%rax,%rax,2), %rsi
movl %ecx, 0x8(%rdx,%rsi,8)
cmpl $0x2, %eax
jb 0x58440
addl $-0x2, %eax
movl 0x34(%r12), %ecx
cmpl %eax, %ecx
jbe 0x58440
movq 0x8(%r12), %rdx
movl %eax, %eax
leaq (%rax,%rax,2), %rax
movl %ecx, 0x8(%rdx,%rax,8)
movl (%rbx), %edx
testl %edx, %edx
je 0x584eb
movq %r12, %rdi
movl $0x80, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x487d8
movl %eax, %ebp
movl 0x28(%r15), %eax
testb $0x40, %al
jne 0x58489
imulq $0x68, %r14, %rax
movq 0x38(%rsp), %rcx
movl 0x3c(%rcx,%rax), %edx
movq %r12, %rdi
movl $0x63, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x487d8
movl 0x28(%r15), %eax
btl $0x9, %eax
jae 0x584a4
movl 0x8(%rbx), %edx
movq %r12, %rdi
movl $0x63, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x487d8
cmpb $0x12, 0x25(%rbx)
jne 0x584ba
movl 0x1c(%rbx), %ecx
movl 0x28(%rbx), %edx
movq %r12, %rdi
movl $0x11, %esi
jmp 0x584c7
movl 0x1c(%rbx), %ecx
movq %r12, %rdi
movl $0x10, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x487d8
testl %ebp, %ebp
js 0x584eb
movl 0x34(%r12), %eax
cmpl %ebp, %eax
jbe 0x584eb
movq 0x8(%r12), %rcx
movl %ebp, %edx
leaq (%rdx,%rdx,2), %rdx
movl %eax, 0x8(%rcx,%rdx,8)
cmpq $0x1, 0x40(%rsp)
jg 0x582a3
jmp 0x58530
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x58332
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
movl 0x30(%rbx), %ebp
movq 0x38(%rbx), %rsi
testl %ebp, %ebp
jle 0x583c1
leaq 0x38(%rbx), %rax
movq %rax, 0x20(%rsp)
jmp 0x5833f
movq 0x8(%rsp), %rax
movl 0x44(%rax), %eax
testl %eax, %eax
jns 0x58552
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x58552
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
movq 0x8(%rsp), %rax
cmpb $0x0, 0x38(%rax)
je 0x58730
movq 0x8(%rsp), %rax
leaq 0x340(%rax), %rbx
addq $0x8, 0x10(%rsp)
xorl %r14d, %r14d
movzbl 0x24(%rbx), %eax
imulq $0x68, %rax, %rax
movq 0x10(%rsp), %rcx
movq 0x20(%rcx,%rax), %r13
movq 0x40(%rbx), %rbp
testb $0x2, 0x48(%r13)
jne 0x58605
cmpq $0x0, 0x18(%r13)
jne 0x58605
movq 0x8(%rsp), %rcx
testb $0x10, 0x32(%rcx)
jne 0x58605
movl 0x28(%rbp), %r15d
movq 0x8(%rsp), %rcx
cmpb $0x0, 0x35(%rcx)
jne 0x585d6
movl %r15d, %ecx
andl $0x40, %ecx
jne 0x585d6
addq 0x10(%rsp), %rax
movl 0x3c(%rax), %edx
movq %r12, %rdi
movl $0x38, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x487d8
andl $0x4300, %r15d          # imm = 0x4300
cmpl $0x200, %r15d           # imm = 0x200
jne 0x58605
movl 0x8(%rbx), %edx
movq 0x8(%rsp), %rax
cmpl 0x50(%rax), %edx
je 0x58605
movq %r12, %rdi
movl $0x38, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x487d8
movl 0x28(%rbp), %eax
testl $0x240, %eax            # imm = 0x240
je 0x58615
addq $0x20, %rbp
jmp 0x58623
btl $0xd, %eax
jae 0x58717
leaq 0x30(%rbx), %rbp
movq (%rbp), %rax
testq %rax, %rax
je 0x58717
movq 0x18(%rsp), %rcx
cmpb $0x0, 0x50(%rcx)
jne 0x58717
movl 0x34(%r12), %ecx
movl 0x20(%rbx), %edx
movq (%r12), %rsi
cmpb $0x0, 0x50(%rsi)
leaq 0x3394a(%rip), %rsi     # 0x8bfa0
jne 0x58670
leal -0x1(%rcx), %esi
testl %edx, %edx
cmovnsl %edx, %esi
movslq %esi, %rsi
leaq (%rsi,%rsi,2), %rsi
shlq $0x3, %rsi
addq 0x8(%r12), %rsi
cmpl %ecx, %edx
jge 0x58717
movl 0x4(%rbx), %edi
leaq 0x10(%r13), %r8
cmpl %edi, 0x4(%rsi)
jne 0x586f0
movzbl (%rsi), %r9d
cmpl $0x62, %r9d
je 0x586e5
cmpl $0x2b, %r9d
jne 0x586f0
movl 0x8(%rsi), %r9d
testb $0x20, 0x48(%r13)
je 0x586c3
movslq %r9d, %r9
movq %r8, %r11
movq (%r11), %r10
movb 0x63(%r10), %bpl
andb $0x3, %bpl
leaq 0x28(%r10), %r11
cmpb $0x2, %bpl
jne 0x586a5
movq 0x8(%r10), %r10
movzwl (%r10,%r9,2), %r9d
movzwl 0x60(%rax), %r11d
testq %r11, %r11
je 0x586f0
movq 0x8(%rax), %r15
xorl %r10d, %r10d
cmpw %r9w, (%r15,%r10,2)
je 0x586fc
incq %r10
cmpq %r10, %r11
jne 0x586d4
jmp 0x586f0
movl 0x8(%rbx), %r9d
movl %r9d, 0x4(%rsi)
movb $0x6b, (%rsi)
incl %edx
addq $0x18, %rsi
cmpl %ecx, %edx
jne 0x5867f
jmp 0x58717
testw %r10w, %r10w
js 0x586f0
andl $0x7fff, %r10d          # imm = 0x7FFF
movl %r10d, 0x8(%rsi)
movl 0x8(%rbx), %r9d
movl %r9d, 0x4(%rsi)
jmp 0x586f0
incl %r14d
addq $0x50, %rbx
movq 0x8(%rsp), %rax
movzbl 0x38(%rax), %eax
cmpl %eax, %r14d
jb 0x58576
movq 0x8(%rsp), %rsi
movl 0x48(%rsi), %eax
movq 0x28(%rsp), %rcx
movl %eax, 0x1f8(%rcx)
movq 0x18(%rsp), %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5b220