// File: /workspace/github_c_makefile_star2_O1/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, -0x50(%rbp)
callq 0x5526f
movzbl 0x38(%r15), %eax
movq %rax, -0x40(%rbp)
testq %rax, %rax
movq %r15, -0x48(%rbp)
je 0x54fcd
leaq 0x338(%r15), %rdi
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x58(%rbp)
movq %rdi, -0x60(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x68(%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 0x54dea
movq 0x40(%r12), %rdx
testq %rdx, %rdx
je 0x54dea
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 0x54e29
movl 0x24(%r14), %edx
movl 0x28(%r14), %ecx
movq %r12, %rdi
xorl %r8d, %r8d
callq 0x45b9e
movq 0x8(%r12), %rax
testq %rax, %rax
je 0x54e29
movb 0x22(%r14), %cl
movslq 0x34(%r12), %rdx
leaq (%rdx,%rdx,2), %rdx
movb %cl, -0x15(%rax,%rdx,8)
testb $0x8, 0x29(%rbx)
je 0x54ef1
cmpl $0x0, 0x30(%r14)
jle 0x54ef1
movl 0x10(%r14), %eax
testl %eax, %eax
jns 0x54e5c
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x54e5c
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
movslq 0x30(%r14), %rax
testq %rax, %rax
jle 0x54ee0
movq 0x38(%r14), %rcx
leaq (%rax,%rax,2), %rdx
leaq (%rcx,%rdx,4), %r13
addq $-0x4, %r13
leal 0x1(%rax), %r15d
movl -0x4(%r13), %ecx
cmpl $-0x1, %ecx
jl 0x54e9d
leal 0x1(%rcx), %edx
movl 0x34(%r12), %eax
cmpl %edx, %eax
jbe 0x54e9d
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 0x45b9e
movl -0x4(%r13), %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, %r13
decl %r15d
cmpl $0x1, %r15d
jg 0x54e79
movq 0x38(%r14), %rsi
movq -0x38(%rbp), %rdi
callq 0x9c3a
movq -0x48(%rbp), %r15
movl 0xc(%r14), %eax
testl %eax, %eax
jns 0x54f0f
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x54f0f
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
movl (%r14), %edx
testl %edx, %edx
je 0x54fbe
movq %r12, %rdi
movl $0x7e, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movl %eax, %r13d
testb $0x40, 0x28(%rbx)
jne 0x54f54
imulq $0x68, -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
movl 0x3c(%rcx,%rax), %edx
movq %r12, %rdi
movl $0x61, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
testb $0x2, 0x29(%rbx)
je 0x54f70
movl 0x8(%r14), %edx
movq %r12, %rdi
movl $0x61, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
cmpb $0x10, 0x21(%r14)
jne 0x54f89
movl 0x18(%r14), %ecx
movl 0x24(%r14), %edx
movq %r12, %rdi
movl $0xf, %esi
jmp 0x54f97
movl 0x18(%r14), %ecx
movq %r12, %rdi
movl $0xe, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x45b9e
testl %r13d, %r13d
js 0x54fbe
movl 0x34(%r12), %eax
cmpl %r13d, %eax
jbe 0x54fbe
movq 0x8(%r12), %rcx
movl %r13d, %edx
leaq (%rdx,%rdx,2), %rdx
movl %eax, 0x8(%rcx,%rdx,8)
cmpq $0x1, -0x68(%rbp)
movq -0x60(%rbp), %rdi
jg 0x54db4
movl 0x44(%r15), %eax
testl %eax, %eax
jns 0x54feb
movq 0x40(%r12), %rcx
testq %rcx, %rcx
je 0x54feb
notl %eax
movl 0x34(%r12), %edx
movl %eax, %eax
movl %edx, (%rcx,%rax,4)
cmpb $0x0, 0x38(%r15)
je 0x55169
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 0x55085
cmpq $0x0, 0x18(%rcx)
jne 0x55085
testb $0x10, 0x32(%r15)
jne 0x55085
movq %r15, %rcx
movl 0x28(%r13), %r15d
cmpb $0x0, 0x35(%rcx)
jne 0x5505c
movl %r15d, %ecx
andl $0x40, %ecx
jne 0x5505c
addq -0x30(%rbp), %rax
movl 0x3c(%rax), %edx
movq %r12, %rdi
movl $0x36, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
andl $0x4300, %r15d          # imm = 0x4300
cmpl $0x200, %r15d           # imm = 0x200
movq -0x48(%rbp), %r15
jne 0x55085
movl 0x8(%rbx), %edx
movq %r12, %rdi
movl $0x36, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movl 0x28(%r13), %eax
testl $0x240, %eax            # imm = 0x240
je 0x55096
addq $0x20, %r13
jmp 0x550a4
btl $0xd, %eax
jb 0x550a0
xorl %eax, %eax
jmp 0x550a8
leaq 0x30(%rbx), %r13
movq (%r13), %rax
testq %rax, %rax
je 0x55154
movq -0x38(%rbp), %rcx
cmpb $0x0, 0x50(%rcx)
jne 0x55154
movl 0x34(%r12), %ecx
movl 0x1c(%rbx), %edx
movq (%r12), %rsi
cmpb $0x0, 0x50(%rsi)
leaq 0x40efa(%rip), %rsi     # 0x95fd0
jne 0x550f0
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 0x55154
movl 0x4(%rbx), %edi
cmpl %edi, 0x4(%rsi)
jne 0x5514a
movzbl (%rsi), %r8d
cmpl $0x60, %r8d
je 0x55131
cmpl $0x29, %r8d
jne 0x5514a
movzwl 0x54(%rax), %r8d
testq %r8, %r8
je 0x5514a
movl 0x8(%rsi), %r9d
movq 0x8(%rax), %r11
xorl %r10d, %r10d
cmpl (%r11,%r10,4), %r9d
je 0x5513e
incq %r10
cmpq %r10, %r8
jne 0x55121
jmp 0x5514a
movl 0x8(%rbx), %r8d
movl %r8d, 0x4(%rsi)
movb $0x69, (%rsi)
jmp 0x5514a
movl %r10d, 0x8(%rsi)
movl 0x8(%rbx), %r8d
movl %r8d, 0x4(%rsi)
incl %edx
addq $0x18, %rsi
cmpl %ecx, %edx
jne 0x550f7
incl %r14d
addq $0x48, %rbx
movzbl 0x38(%r15), %eax
cmpl %eax, %r14d
jb 0x55005
movl 0x48(%r15), %eax
movq -0x50(%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 0x579c0