// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/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.
  */
  sqlite3ExprCacheClear(pParse);
  for(i=pWInfo->nLevel-1; i>=0; i--){
    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->iLeftJoin ){
      int addr;
      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);
    }
  }

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

  /* Close all of the cursors that were opened by sqlite3WhereBegin.
  */
  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;
    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 ){
        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, j, 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 ){
          for(j=0; j<pIdx->nColumn; j++){
            if( pOp->p2==pIdx->aiColumn[j] ){
              pOp->p2 = j;
              pOp->p1 = pLevel->iIdxCur;
              break;
            }
          }
          assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || j<pIdx->nColumn );
        }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
movq %rsp, %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, -0x38(%rbp)
movq 0x10(%rdi), %r12
movq %rdi, -0x58(%rbp)
callq 0x4da57
movzbl 0x38(%rbx), %eax
movq %rbx, -0x30(%rbp)
leaq 0x338(%rbx), %r15
addq $0x8, %r14
movq %r14, -0x40(%rbp)
movq %rax, %r14
testq %r14, %r14
jle 0x4d800
decq %r14
imulq $0x48, %r14, %rbx
movq 0x40(%r15,%rbx), %r13
movl 0x14(%r15,%rbx), %eax
testl %eax, %eax
jns 0x4d640
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x4d640
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
addq %r15, %rbx
movzbl 0x21(%rbx), %esi
cmpl $0x95, %esi
je 0x4d677
movl 0x24(%rbx), %edx
movl 0x28(%rbx), %ecx
movq %r12, %rdi
callq 0x41f42
movq 0x8(%r12), %rax
testq %rax, %rax
je 0x4d677
movb 0x22(%rbx), %cl
movslq 0x34(%r12), %rdx
imulq $0x18, %rdx, %rdx
movb %cl, -0x15(%rax,%rdx)
movq %r13, -0x48(%rbp)
testb $0x8, 0x29(%r13)
je 0x4d740
movl 0x30(%rbx), %r13d
testl %r13d, %r13d
jle 0x4d740
movl 0x10(%rbx), %eax
testl %eax, %eax
movq %r14, -0x60(%rbp)
jns 0x4d6b8
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x4d6b8
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
movl 0x30(%rbx), %r13d
movq 0x38(%rbx), %rax
movslq %r13d, %rcx
imulq $0xc, %rcx, %rcx
leaq (%rax,%rcx), %r14
addq $-0x4, %r14
testl %r13d, %r13d
jle 0x4d72f
movl -0x4(%r14), %ecx
cmpl $-0x1, %ecx
jl 0x4d6f4
leal 0x1(%rcx), %edx
movl 0x34(%r12), %eax
cmpl %edx, %eax
jbe 0x4d6f4
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 0x41f42
movl -0x4(%r14), %eax
testl %eax, %eax
jle 0x4d726
decl %eax
movl 0x34(%r12), %ecx
cmpl %eax, %ecx
jbe 0x4d726
movq 0x8(%r12), %rdx
movl %eax, %eax
imulq $0x18, %rax, %rax
movl %ecx, 0x8(%rdx,%rax)
decl %r13d
addq $-0xc, %r14
jmp 0x4d6cb
movq 0x38(%rbx), %rsi
movq -0x38(%rbp), %rdi
callq 0x968b
movq -0x60(%rbp), %r14
movl 0xc(%rbx), %eax
testl %eax, %eax
jns 0x4d75d
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x4d75d
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
movl (%rbx), %edx
testl %edx, %edx
je 0x4d60c
movq %r12, %rdi
pushq $0x7e
popq %rsi
callq 0x42436
movl %eax, -0x4c(%rbp)
movq -0x48(%rbp), %r13
movl 0x28(%r13), %eax
testb $0x40, %al
jne 0x4d79c
imulq $0x68, %r14, %rax
movq -0x40(%rbp), %rcx
movl 0x3c(%rcx,%rax), %edx
movq %r12, %rdi
pushq $0x61
popq %rsi
callq 0x42436
movl 0x28(%r13), %eax
btl $0x9, %eax
jae 0x4d7b0
movl 0x8(%rbx), %edx
movq %r12, %rdi
pushq $0x61
popq %rsi
callq 0x42436
cmpb $0x10, 0x21(%rbx)
jne 0x4d7c4
movl 0x18(%rbx), %ecx
movl 0x24(%rbx), %edx
movq %r12, %rdi
pushq $0xf
popq %rsi
jmp 0x4d7cf
movl 0x18(%rbx), %ecx
movq %r12, %rdi
pushq $0xe
popq %rsi
xorl %edx, %edx
callq 0x41f42
movl -0x4c(%rbp), %edx
testl %edx, %edx
js 0x4d60c
movl 0x34(%r12), %eax
cmpl %edx, %eax
jbe 0x4d60c
movq 0x8(%r12), %rcx
movl %edx, %edx
imulq $0x18, %rdx, %rdx
movl %eax, 0x8(%rcx,%rdx)
jmp 0x4d60c
movq -0x30(%rbp), %rax
movl 0x44(%rax), %eax
testl %eax, %eax
jns 0x4d821
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x4d821
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
xorl %r8d, %r8d
movq -0x30(%rbp), %rax
movzbl 0x38(%rax), %eax
cmpl %eax, %r8d
jae 0x4d95f
movzbl 0x20(%r15), %eax
imulq $0x68, %rax, %rax
movq -0x40(%rbp), %rcx
movq 0x20(%rcx,%rax), %rcx
movq 0x40(%r15), %rbx
testb $0x2, 0x46(%rcx)
jne 0x4d8b5
cmpq $0x0, 0x18(%rcx)
jne 0x4d8b5
movq -0x30(%rbp), %rcx
testb $0x10, 0x32(%rcx)
jne 0x4d8b5
movl 0x28(%rbx), %r14d
movq -0x30(%rbp), %rcx
cmpb $0x0, 0x35(%rcx)
jne 0x4d890
movl %r14d, %ecx
andl $0x40, %ecx
jne 0x4d890
addq -0x40(%rbp), %rax
movl 0x3c(%rax), %edx
movq %r12, %rdi
pushq $0x36
popq %rsi
movq %r8, %r13
callq 0x42436
movq %r13, %r8
andl $0x4300, %r14d          # imm = 0x4300
cmpl $0x200, %r14d           # imm = 0x200
jne 0x4d8b5
movl 0x8(%r15), %edx
movq %r12, %rdi
pushq $0x36
popq %rsi
movq %r8, %r14
callq 0x42436
movq %r14, %r8
movl 0x28(%rbx), %eax
testw $0x240, %ax             # imm = 0x240
je 0x4d8c4
addq $0x20, %rbx
jmp 0x4d8ce
btl $0xd, %eax
jae 0x4d8e0
leaq 0x30(%r15), %rbx
movq (%rbx), %r14
testq %r14, %r14
je 0x4d8e0
movq -0x38(%rbp), %rax
cmpb $0x0, 0x50(%rax)
je 0x4d8ec
incl %r8d
addq $0x48, %r15
jmp 0x4d824
movl 0x34(%r12), %ebx
movl 0x1c(%r15), %r13d
movq %r12, %rdi
movl %r13d, %esi
movq %r8, -0x48(%rbp)
callq 0x4a93d
movq -0x48(%rbp), %r8
cmpl %ebx, %r13d
jge 0x4d8e0
movl 0x4(%rax), %ecx
cmpl 0x4(%r15), %ecx
jne 0x4d956
movzbl (%rax), %ecx
cmpl $0x60, %ecx
je 0x4d940
cmpl $0x29, %ecx
jne 0x4d956
movzwl 0x54(%r14), %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x4d956
movl 0x8(%rax), %esi
movq 0x8(%r14), %rdi
cmpl (%rdi,%rdx,4), %esi
je 0x4d94c
incq %rdx
jmp 0x4d92a
movl 0x8(%r15), %ecx
movl %ecx, 0x4(%rax)
movb $0x69, (%rax)
jmp 0x4d956
movl %edx, 0x8(%rax)
movl 0x8(%r15), %ecx
movl %ecx, 0x4(%rax)
incl %r13d
addq $0x18, %rax
jmp 0x4d908
movq -0x30(%rbp), %rsi
movl 0x48(%rsi), %eax
movq -0x58(%rbp), %rcx
movl %eax, 0x1e8(%rcx)
movq -0x38(%rbp), %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4f26f