// File: /workspace/github_c_makefile_star2_O1/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), %rax
movq %rax, 0x8(%rsp)
movq (%rdi), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rdi), %r12
movq %rdi, 0x18(%rsp)
callq 0x5813a
movq %rbx, (%rsp)
movzbl 0x38(%rbx), %r14d
testq %r14, %r14
je 0x57e3f
movq (%rsp), %rax
leaq 0x340(%rax), %r13
movq 0x8(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x20(%rsp)
movq %r13, 0x28(%rsp)
movq %r14, 0x30(%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 0x57c18
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x57c18
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 0x57c52
movl 0x28(%rbx), %edx
movl 0x2c(%rbx), %ecx
movq %r12, %rdi
xorl %r8d, %r8d
callq 0x47fb1
movq 0x8(%r12), %rax
testq %rax, %rax
je 0x57c52
movb 0x26(%rbx), %cl
movslq 0x34(%r12), %rdx
leaq (%rdx,%rdx,2), %rdx
movb %cl, -0x15(%rax,%rdx,8)
testb $0x8, 0x29(%r15)
je 0x57d17
cmpl $0x0, 0x30(%rbx)
jle 0x57d17
movl 0x10(%rbx), %eax
testl %eax, %eax
jns 0x57c84
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x57c84
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
movslq 0x30(%rbx), %rax
testq %rax, %rax
jle 0x57d04
movq 0x38(%rbx), %rcx
leaq (%rax,%rax,2), %rdx
leaq (%rcx,%rdx,4), %rbp
addq $-0x4, %rbp
leal 0x1(%rax), %r13d
movl -0x4(%rbp), %ecx
cmpl $-0x1, %ecx
jl 0x57cc4
leal 0x1(%rcx), %edx
movl 0x34(%r12), %eax
cmpl %edx, %eax
jbe 0x57cc4
movq 0x8(%r12), %rsi
movl %edx, %edx
leaq (%rdx,%rdx,2), %rdx
movl %eax, 0x8(%rsi,%rdx,8)
movzbl (%rbp), %esi
movl -0x8(%rbp), %edx
movq %r12, %rdi
xorl %r8d, %r8d
callq 0x47fb1
movl -0x4(%rbp), %eax
testl %eax, %eax
jle 0x57cf7
decl %eax
movl 0x34(%r12), %ecx
cmpl %eax, %ecx
jbe 0x57cf7
movq 0x8(%r12), %rdx
movl %eax, %eax
leaq (%rax,%rax,2), %rax
movl %ecx, 0x8(%rdx,%rax,8)
addq $-0xc, %rbp
decl %r13d
cmpl $0x1, %r13d
jg 0x57ca1
movq 0x38(%rbx), %rsi
movq 0x10(%rsp), %rdi
callq 0x9b58
movq 0x28(%rsp), %r13
movl 0xc(%rbx), %eax
testl %eax, %eax
jns 0x57d34
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x57d34
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
movl 0x14(%rbx), %ecx
testl %ecx, %ecx
je 0x57d8c
movq %r12, %rdi
movl $0x10, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x47fb1
movslq 0x14(%rbx), %rax
testq %rax, %rax
js 0x57d6c
movl 0x34(%r12), %ecx
cmpl %eax, %ecx
jbe 0x57d6c
movq 0x8(%r12), %rdx
leaq (%rax,%rax,2), %rsi
movl %ecx, 0x8(%rdx,%rsi,8)
cmpl $0x2, %eax
jl 0x57d8c
addl $-0x2, %eax
movl 0x34(%r12), %ecx
cmpl %eax, %ecx
jbe 0x57d8c
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 0x57e33
movq %r12, %rdi
movl $0x80, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x47fb1
movl %eax, %ebp
testb $0x40, 0x28(%r15)
jne 0x57dd0
imulq $0x68, %r14, %rax
movq 0x20(%rsp), %rcx
movl 0x3c(%rcx,%rax), %edx
movq %r12, %rdi
movl $0x63, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x47fb1
testb $0x2, 0x29(%r15)
je 0x57dec
movl 0x8(%rbx), %edx
movq %r12, %rdi
movl $0x63, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x47fb1
cmpb $0x12, 0x25(%rbx)
jne 0x57e02
movl 0x1c(%rbx), %ecx
movl 0x28(%rbx), %edx
movq %r12, %rdi
movl $0x11, %esi
jmp 0x57e0f
movl 0x1c(%rbx), %ecx
movq %r12, %rdi
movl $0x10, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x47fb1
testl %ebp, %ebp
js 0x57e33
movl 0x34(%r12), %eax
cmpl %ebp, %eax
jbe 0x57e33
movq 0x8(%r12), %rcx
movl %ebp, %edx
leaq (%rdx,%rdx,2), %rdx
movl %eax, 0x8(%rcx,%rdx,8)
cmpq $0x1, 0x30(%rsp)
jg 0x57be4
movq (%rsp), %r15
movl 0x44(%r15), %eax
testl %eax, %eax
jns 0x57e61
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x57e61
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
cmpb $0x0, 0x38(%r15)
je 0x58040
leaq 0x340(%r15), %rbx
addq $0x8, 0x8(%rsp)
xorl %r14d, %r14d
movzbl 0x24(%rbx), %eax
imulq $0x68, %rax, %rax
movq 0x8(%rsp), %rcx
movq 0x20(%rcx,%rax), %r13
movq 0x40(%rbx), %rbp
testb $0x2, 0x48(%r13)
jne 0x57f05
cmpq $0x0, 0x18(%r13)
jne 0x57f05
testb $0x10, 0x32(%r15)
jne 0x57f05
movq %r15, %rcx
movl 0x28(%rbp), %r15d
cmpb $0x0, 0x35(%rcx)
jne 0x57ed6
movl %r15d, %ecx
andl $0x40, %ecx
jne 0x57ed6
addq 0x8(%rsp), %rax
movl 0x3c(%rax), %edx
movq %r12, %rdi
movl $0x38, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x47fb1
andl $0x4300, %r15d          # imm = 0x4300
cmpl $0x200, %r15d           # imm = 0x200
movq (%rsp), %r15
jne 0x57f05
movl 0x8(%rbx), %edx
cmpl 0x50(%r15), %edx
je 0x57f05
movq %r12, %rdi
movl $0x38, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x47fb1
movl 0x28(%rbp), %eax
testl $0x240, %eax            # imm = 0x240
je 0x57f15
addq $0x20, %rbp
jmp 0x57f23
btl $0xd, %eax
jb 0x57f1f
xorl %eax, %eax
jmp 0x57f27
leaq 0x30(%rbx), %rbp
movq (%rbp), %rax
testq %rax, %rax
je 0x5802b
movq 0x10(%rsp), %rcx
cmpb $0x0, 0x50(%rcx)
jne 0x5802b
movl 0x34(%r12), %ecx
movl 0x20(%rbx), %edx
movq (%r12), %rsi
cmpb $0x0, 0x50(%rsi)
leaq 0x3304a(%rip), %rsi     # 0x8afa0
jne 0x57f70
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 0x5802b
movl 0x4(%rbx), %edi
cmpl %edi, 0x4(%rsi)
jne 0x5801d
movzbl (%rsi), %r8d
cmpl $0x62, %r8d
je 0x57fc0
cmpl $0x2b, %r8d
jne 0x5801d
movl 0x8(%rsi), %r8d
testb $0x20, 0x48(%r13)
je 0x57fd9
movq 0x10(%r13), %r9
testq %r9, %r9
je 0x57fcd
movb 0x63(%r9), %r10b
andb $0x3, %r10b
cmpb $0x2, %r10b
je 0x57fcd
movq 0x28(%r9), %r9
jmp 0x57fa7
movl 0x8(%rbx), %r8d
movl %r8d, 0x4(%rsi)
movb $0x6b, (%rsi)
jmp 0x5801d
movslq %r8d, %r8
movq 0x8(%r9), %r9
movzwl (%r9,%r8,2), %r8d
movzwl 0x60(%rax), %r10d
movw $0xffff, %r9w           # imm = 0xFFFF
testq %r10, %r10
je 0x58007
movq 0x8(%rax), %r15
xorl %r11d, %r11d
cmpw %r8w, (%r15,%r11,2)
je 0x58000
incq %r11
cmpq %r11, %r10
jne 0x57fef
jmp 0x58003
movl %r11d, %r9d
movq (%rsp), %r15
testw %r9w, %r9w
js 0x5801d
movzwl %r9w, %r8d
movl %r8d, 0x8(%rsi)
movl 0x8(%rbx), %r8d
movl %r8d, 0x4(%rsi)
incl %edx
addq $0x18, %rsi
cmpl %ecx, %edx
jne 0x57f7b
incl %r14d
addq $0x50, %rbx
movzbl 0x38(%r15), %eax
cmpl %eax, %r14d
jb 0x57e7c
movl 0x48(%r15), %eax
movq 0x18(%rsp), %rcx
movl %eax, 0x1f8(%rcx)
movq 0x10(%rsp), %rdi
movq %r15, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5a97f