// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movq %rdi, %r15
movq (%rdi), %rdi
movq 0x8(%r15), %rax
movq %rax, -0x30(%rbp)
movq (%rdi), %rax
movq %rax, -0x38(%rbp)
movq 0x10(%rdi), %r12
movq %rdi, -0x58(%rbp)
callq 0x552af
movzbl 0x38(%r15), %eax
movq %rax, -0x40(%rbp)
testq %rax, %rax
movq %r15, -0x48(%rbp)
je 0x5500e
leaq 0x338(%r15), %rdi
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x60(%rbp)
movq %rdi, -0x68(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x70(%rbp)
decq %rax
movq %rax, -0x40(%rbp)
leaq (%rax,%rax,8), %rax
movq 0x40(%rdi,%rax,8), %rbx
movl 0x14(%rdi,%rax,8), %ecx
testl %ecx, %ecx
jns 0x54dfa
movq 0x40(%r12), %rdx
testq %rdx, %rdx
je 0x54dfa
notl %ecx
movl 0x34(%r12), %esi
movl %ecx, %ecx
movl %esi, (%rdx,%rcx,4)
leaq (%rdi,%rax,8), %r14
movzbl 0x21(%r14), %esi
cmpl $0x95, %esi
je 0x54e39
movl 0x24(%r14), %edx
movl 0x28(%r14), %ecx
movq %r12, %rdi
xorl %r8d, %r8d
callq 0x45f15
movq 0x8(%r12), %rax
testq %rax, %rax
je 0x54e39
movb 0x22(%r14), %cl
movslq 0x34(%r12), %rdx
leaq (%rdx,%rdx,2), %rdx
movb %cl, -0x15(%rax,%rdx,8)
testb $0x8, 0x29(%rbx)
je 0x54ef4
movl 0x30(%r14), %r13d
testl %r13d, %r13d
jle 0x54ef4
movl 0x10(%r14), %eax
testl %eax, %eax
js 0x54fd6
leaq 0x38(%r14), %rax
movq %rax, -0x50(%rbp)
movq 0x38(%r14), %rsi
movl %r13d, %eax
leaq (%rax,%rax,2), %rax
leaq (%rsi,%rax,4), %r15
addq $-0x4, %r15
incl %r13d
movl -0x4(%r15), %ecx
cmpl $-0x1, %ecx
jl 0x54e9e
leal 0x1(%rcx), %edx
movl 0x34(%r12), %eax
cmpl %edx, %eax
jbe 0x54e9e
movq 0x8(%r12), %rsi
movl %edx, %edx
leaq (%rdx,%rdx,2), %rdx
movl %eax, 0x8(%rsi,%rdx,8)
movzbl (%r15), %esi
movl -0x8(%r15), %edx
movq %r12, %rdi
xorl %r8d, %r8d
callq 0x45f15
movl -0x4(%r15), %eax
testl %eax, %eax
jle 0x54ed3
decl %eax
movl 0x34(%r12), %ecx
cmpl %eax, %ecx
jbe 0x54ed3
movq 0x8(%r12), %rdx
movl %eax, %eax
leaq (%rax,%rax,2), %rax
movl %ecx, 0x8(%rdx,%rax,8)
addq $-0xc, %r15
decl %r13d
cmpl $0x1, %r13d
jg 0x54e7a
movq -0x50(%rbp), %rax
movq (%rax), %rsi
movq -0x48(%rbp), %r15
movq -0x38(%rbp), %rdi
callq 0x9ca4
movl 0xc(%r14), %eax
testl %eax, %eax
jns 0x54f12
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x54f12
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
movl (%r14), %edx
testl %edx, %edx
je 0x54fc5
movq %r12, %rdi
movl $0x7e, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45f15
movl %eax, %r13d
movl 0x28(%rbx), %eax
testb $0x40, %al
jne 0x54f5b
imulq $0x68, -0x40(%rbp), %rax
movq -0x60(%rbp), %rcx
movl 0x3c(%rcx,%rax), %edx
movq %r12, %rdi
movl $0x61, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45f15
movl 0x28(%rbx), %eax
btl $0x9, %eax
jae 0x54f77
movl 0x8(%r14), %edx
movq %r12, %rdi
movl $0x61, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45f15
cmpb $0x10, 0x21(%r14)
jne 0x54f90
movl 0x18(%r14), %ecx
movl 0x24(%r14), %edx
movq %r12, %rdi
movl $0xf, %esi
jmp 0x54f9e
movl 0x18(%r14), %ecx
movq %r12, %rdi
movl $0xe, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x45f15
testl %r13d, %r13d
js 0x54fc5
movl 0x34(%r12), %eax
cmpl %r13d, %eax
jbe 0x54fc5
movq 0x8(%r12), %rcx
movl %r13d, %edx
leaq (%rdx,%rdx,2), %rdx
movl %eax, 0x8(%rcx,%rdx,8)
cmpq $0x1, -0x70(%rbp)
movq -0x68(%rbp), %rdi
jg 0x54dc4
jmp 0x5500e
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x54e5c
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
movl 0x30(%r14), %r13d
movq 0x38(%r14), %rsi
testl %r13d, %r13d
jle 0x54eeb
leaq 0x38(%r14), %rax
movq %rax, -0x50(%rbp)
jmp 0x54e68
movl 0x44(%r15), %eax
testl %eax, %eax
jns 0x5502c
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x5502c
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
cmpb $0x0, 0x38(%r15)
je 0x551aa
leaq 0x338(%r15), %rbx
addq $0x8, -0x30(%rbp)
xorl %r14d, %r14d
movzbl 0x20(%rbx), %eax
imulq $0x68, %rax, %rax
movq -0x30(%rbp), %rcx
movq 0x20(%rcx,%rax), %rcx
movq 0x40(%rbx), %r13
testb $0x2, 0x46(%rcx)
jne 0x550c6
cmpq $0x0, 0x18(%rcx)
jne 0x550c6
testb $0x10, 0x32(%r15)
jne 0x550c6
movq %r15, %rcx
movl 0x28(%r13), %r15d
cmpb $0x0, 0x35(%rcx)
jne 0x5509d
movl %r15d, %ecx
andl $0x40, %ecx
jne 0x5509d
addq -0x30(%rbp), %rax
movl 0x3c(%rax), %edx
movq %r12, %rdi
movl $0x36, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45f15
andl $0x4300, %r15d          # imm = 0x4300
cmpl $0x200, %r15d           # imm = 0x200
movq -0x48(%rbp), %r15
jne 0x550c6
movl 0x8(%rbx), %edx
movq %r12, %rdi
movl $0x36, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45f15
movl 0x28(%r13), %eax
testl $0x240, %eax            # imm = 0x240
je 0x550d7
addq $0x20, %r13
jmp 0x550e5
btl $0xd, %eax
jae 0x55195
leaq 0x30(%rbx), %r13
movq (%r13), %rax
testq %rax, %rax
je 0x55195
movq -0x38(%rbp), %rcx
cmpb $0x0, 0x50(%rcx)
jne 0x55195
movl 0x34(%r12), %ecx
movl 0x1c(%rbx), %edx
movq (%r12), %rsi
cmpb $0x0, 0x50(%rsi)
leaq 0x40eb9(%rip), %rsi     # 0x95fd0
jne 0x55131
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 0x55195
movl 0x4(%rbx), %edi
cmpl %edi, 0x4(%rsi)
jne 0x5518b
movzbl (%rsi), %r8d
cmpl $0x60, %r8d
je 0x55172
cmpl $0x29, %r8d
jne 0x5518b
movzwl 0x54(%rax), %r8d
testq %r8, %r8
je 0x5518b
movl 0x8(%rsi), %r9d
movq 0x8(%rax), %r11
xorl %r10d, %r10d
cmpl (%r11,%r10,4), %r9d
je 0x5517f
incq %r10
cmpq %r10, %r8
jne 0x55162
jmp 0x5518b
movl 0x8(%rbx), %r8d
movl %r8d, 0x4(%rsi)
movb $0x69, (%rsi)
jmp 0x5518b
movl %r10d, 0x8(%rsi)
movl 0x8(%rbx), %r8d
movl %r8d, 0x4(%rsi)
incl %edx
addq $0x18, %rsi
cmpl %ecx, %edx
jne 0x55138
incl %r14d
addq $0x48, %rbx
movzbl 0x38(%r15), %eax
cmpl %eax, %r14d
jb 0x55046
movl 0x48(%r15), %eax
movq -0x58(%rbp), %rcx
movl %eax, 0x1e8(%rcx)
movq -0x38(%rbp), %rdi
movq %r15, %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x579ca