// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: sqlite3WindowRewrite
SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
  int rc = SQLITE_OK;
  if( p->pWin && p->pPrior==0 && (p->selFlags & SF_WinRewrite)==0 ){
    Vdbe *v = sqlite3GetVdbe(pParse);
    sqlite3 *db = pParse->db;
    Select *pSub = 0;             /* The subquery */
    SrcList *pSrc = p->pSrc;
    Expr *pWhere = p->pWhere;
    ExprList *pGroupBy = p->pGroupBy;
    Expr *pHaving = p->pHaving;
    ExprList *pSort = 0;

    ExprList *pSublist = 0;       /* Expression list for sub-query */
    Window *pMWin = p->pWin;      /* Main window object */
    Window *pWin;                 /* Window object iterator */
    Table *pTab;
    Walker w;

    u32 selFlags = p->selFlags;

    pTab = sqlite3DbMallocZero(db, sizeof(Table));
    if( pTab==0 ){
      return sqlite3ErrorToParser(db, SQLITE_NOMEM);
    }
    sqlite3AggInfoPersistWalkerInit(&w, pParse);
    sqlite3WalkSelect(&w, p);

    p->pSrc = 0;
    p->pWhere = 0;
    p->pGroupBy = 0;
    p->pHaving = 0;
    p->selFlags &= ~SF_Aggregate;
    p->selFlags |= SF_WinRewrite;

    /* Create the ORDER BY clause for the sub-select. This is the concatenation
    ** of the window PARTITION and ORDER BY clauses. Then, if this makes it
    ** redundant, remove the ORDER BY from the parent SELECT.  */
    pSort = exprListAppendList(pParse, 0, pMWin->pPartition, 1);
    pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1);
    if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){
      int nSave = pSort->nExpr;
      pSort->nExpr = p->pOrderBy->nExpr;
      if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
        sqlite3ExprListDelete(db, p->pOrderBy);
        p->pOrderBy = 0;
      }
      pSort->nExpr = nSave;
    }

    /* Assign a cursor number for the ephemeral table used to buffer rows.
    ** The OpenEphemeral instruction is coded later, after it is known how
    ** many columns the table will have.  */
    pMWin->iEphCsr = pParse->nTab++;
    pParse->nTab += 3;

    selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, pTab, &pSublist);
    selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, pTab, &pSublist);
    pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);

    /* Append the PARTITION BY and ORDER BY expressions to the to the
    ** sub-select expression list. They are required to figure out where
    ** boundaries for partitions and sets of peer rows lie.  */
    pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition, 0);
    pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy, 0);

    /* Append the arguments passed to each window function to the
    ** sub-select expression list. Also allocate two registers for each
    ** window function - one for the accumulator, another for interim
    ** results.  */
    for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
      ExprList *pArgs = pWin->pOwner->x.pList;
      if( pWin->pFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
        selectWindowRewriteEList(pParse, pMWin, pSrc, pArgs, pTab, &pSublist);
        pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
        pWin->bExprArgs = 1;
      }else{
        pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
        pSublist = exprListAppendList(pParse, pSublist, pArgs, 0);
      }
      if( pWin->pFilter ){
        Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
        pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter);
      }
      pWin->regAccum = ++pParse->nMem;
      pWin->regResult = ++pParse->nMem;
      sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
    }

    /* If there is no ORDER BY or PARTITION BY clause, and the window
    ** function accepts zero arguments, and there are no other columns
    ** selected (e.g. "SELECT row_number() OVER () FROM t1"), it is possible
    ** that pSublist is still NULL here. Add a constant expression here to
    ** keep everything legal in this case.
    */
    if( pSublist==0 ){
      pSublist = sqlite3ExprListAppend(pParse, 0,
        sqlite3Expr(db, TK_INTEGER, "0")
      );
    }

    pSub = sqlite3SelectNew(
        pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
    );
    SELECTTRACE(1,pParse,pSub,
       ("New window-function subquery in FROM clause of (%u/%p)\n",
       p->selId, p));
    p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
    if( p->pSrc ){
      Table *pTab2;
      p->pSrc->a[0].pSelect = pSub;
      sqlite3SrcListAssignCursors(pParse, p->pSrc);
      pSub->selFlags |= SF_Expanded;
      pTab2 = sqlite3ResultSetOfSelect(pParse, pSub, SQLITE_AFF_NONE);
      pSub->selFlags |= (selFlags & SF_Aggregate);
      if( pTab2==0 ){
        /* Might actually be some other kind of error, but in that case
        ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
        ** the correct error message regardless. */
        rc = SQLITE_NOMEM;
      }else{
        memcpy(pTab, pTab2, sizeof(Table));
        pTab->tabFlags |= TF_Ephemeral;
        p->pSrc->a[0].pTab = pTab;
        pTab = pTab2;
        memset(&w, 0, sizeof(w));
        w.xExprCallback = sqlite3WindowExtraAggFuncDepth;
        w.xSelectCallback = sqlite3WalkerDepthIncrease;
        w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
        sqlite3WalkSelect(&w, pSub);
      }
    }else{
      sqlite3SelectDelete(db, pSub);
    }
    if( db->mallocFailed ) rc = SQLITE_NOMEM;
    sqlite3DbFree(db, pTab);
  }

  if( rc ){
    if( pParse->nErr==0 ){
      assert( pParse->db->mallocFailed );
      sqlite3ErrorToParser(pParse->db, SQLITE_NOMEM);
    }
  }
  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
xorl %r15d, %r15d
cmpq $0x0, 0x70(%rsi)
je 0x2cb9ad
movq %rsi, %rbp
cmpq $0x0, 0x50(%rsi)
jne 0x2cb9ad
testb $0x10, 0x6(%rbp)
jne 0x2cb9ad
movq %rdi, %rbx
callq 0x2bc630
movq %rax, %r13
movq (%rbx), %r14
movq 0x28(%rbp), %rax
movq %rax, 0x8(%rsp)
movq 0x30(%rbp), %rax
movq %rax, 0x40(%rsp)
movq 0x38(%rbp), %rax
movq %rax, 0x38(%rsp)
movq 0x40(%rbp), %rax
movq %rax, 0x30(%rsp)
movq 0x70(%rbp), %r12
movl 0x4(%rbp), %r15d
movl $0x80, %esi
movq %r14, %rdi
callq 0x279b27
testq %rax, %rax
je 0x2cb924
movl %r15d, 0x1c(%rsp)
movq %rax, (%rsp)
movq %r14, 0x10(%rsp)
leaq 0x28(%rbp), %r14
xorps %xmm0, %xmm0
leaq 0xa0(%rsp), %rdi
movups %xmm0, 0x18(%rdi)
movq $0x0, 0x28(%rdi)
movq %rbx, (%rdi)
leaq 0x737a(%rip), %rax      # 0x2d28a3
movq %rax, 0x8(%rdi)
leaq -0x2069(%rip), %rax     # 0x2c94cb
movq %rax, 0x10(%rdi)
movq %rbp, %rsi
callq 0x2c0b5b
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movq %r14, 0x28(%rsp)
movups %xmm0, 0x10(%r14)
movl $0xffeffff7, %eax       # imm = 0xFFEFFFF7
andl 0x4(%rbp), %eax
orl $0x100000, %eax         # imm = 0x100000
movl %eax, 0x4(%rbp)
movq 0x10(%r12), %rdx
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1, %ecx
callq 0x2d2759
movq 0x18(%r12), %rdx
movq %rbx, %rdi
movq %rax, %rsi
movl $0x1, %ecx
callq 0x2d2759
movq %rax, %r14
testq %rax, %rax
je 0x2cb5d7
movq 0x48(%rbp), %rsi
testq %rsi, %rsi
je 0x2cb5d7
movl (%rsi), %eax
movl (%r14), %r15d
cmpl %r15d, %eax
jg 0x2cb5d7
movl %eax, (%r14)
movq %r14, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x2c128a
testl %eax, %eax
jne 0x2cb5d4
movq 0x48(%rbp), %rsi
testq %rsi, %rsi
je 0x2cb5cc
movq 0x10(%rsp), %rdi
callq 0x2a65a3
movq $0x0, 0x48(%rbp)
movl %r15d, (%r14)
movq %r14, 0x20(%rsp)
movl 0x34(%rbx), %eax
movl %eax, 0x58(%r12)
addl $0x4, %eax
movl %eax, 0x34(%rbx)
movq 0x20(%rbp), %rsi
leaq 0x70(%rsp), %r15
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r15)
leaq 0x48(%rsp), %r14
xorl %eax, %eax
movq %rax, 0x20(%r14)
movq %rax, 0x10(%r14)
movq %r12, (%r14)
movq 0x8(%rsp), %rax
movq %rax, 0x8(%r14)
movq (%rsp), %rax
movq %rax, 0x18(%r14)
movq %rbx, (%r15)
leaq 0x730a(%rip), %rax      # 0x2d2932
movq %rax, 0x8(%r15)
leaq 0x74bc(%rip), %rax      # 0x2d2aef
movq %rax, 0x10(%r15)
movq %r14, 0x28(%r15)
movq %r15, %rdi
callq 0x2c0cc0
movq 0x10(%r14), %rax
movq 0x48(%rbp), %rsi
xorl %ebp, %ebp
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r15)
movq %rbp, 0x20(%r14)
movq %rax, 0x10(%r14)
movq %r12, (%r14)
movq 0x8(%rsp), %rax
movq %rax, 0x8(%r14)
movq (%rsp), %rax
movq %rax, 0x18(%r14)
movq %rbx, (%r15)
leaq 0x72b7(%rip), %rax      # 0x2d2932
movq %rax, 0x8(%r15)
leaq 0x7469(%rip), %rax      # 0x2d2aef
movq %rax, 0x10(%r15)
movq %r14, 0x28(%r15)
leaq 0x70(%rsp), %rdi
callq 0x2c0cc0
movq 0x10(%r14), %rsi
testq %rsi, %rsi
je 0x2cb6a3
movl (%rsi), %ebp
movl %ebp, 0x78(%r12)
movq 0x10(%r12), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x2d2759
movq 0x18(%r12), %rdx
movq %rbx, %rdi
movq %rax, %rsi
xorl %ecx, %ecx
callq 0x2d2759
movq %rax, %r15
leaq 0x70(%rsp), %rbp
movq %r12, %r14
movq 0x50(%r14), %rax
movq 0x70(%r14), %rcx
movq 0x20(%rcx), %rdx
testb $0x10, 0x6(%rax)
jne 0x2cb6f4
testq %r15, %r15
je 0x2cb76f
movl (%r15), %eax
jmp 0x2cb771
leaq 0x88(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x68(%rsp)
movq %r15, 0x58(%rsp)
movq %r12, 0x48(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x50(%rsp)
movq (%rsp), %rax
movq %rax, 0x60(%rsp)
movq %rbx, 0x70(%rsp)
leaq 0x71fe(%rip), %rax      # 0x2d2932
movq %rax, 0x78(%rsp)
leaq 0x73af(%rip), %rax      # 0x2d2aef
movq %rax, 0x80(%rsp)
leaq 0x48(%rsp), %rax
movq %rax, 0x98(%rsp)
movq %rbp, %rdi
movq %rdx, %rsi
callq 0x2c0cc0
movq 0x58(%rsp), %r15
testq %r15, %r15
je 0x2cb787
movl (%r15), %eax
jmp 0x2cb789
xorl %eax, %eax
movl %eax, 0x7c(%r14)
movq %rbx, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x2d2759
movq %rax, %r15
jmp 0x2cb795
xorl %eax, %eax
movl %eax, 0x7c(%r14)
movb $0x1, 0x8c(%r14)
movq 0x48(%r14), %rsi
testq %rsi, %rsi
je 0x2cb7bd
movq 0x10(%rsp), %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2c2864
movq (%rbx), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x2b1a3f
movq %rax, %r15
movl 0x38(%rbx), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x5c(%r14)
addl $0x2, %eax
movl %eax, 0x38(%rbx)
movl %eax, 0x60(%r14)
movq %r13, %rdi
movl $0x48, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x2a83e1
movq 0x40(%r14), %r14
testq %r14, %r14
jne 0x2cb6d4
testq %r15, %r15
movq 0x10(%rsp), %r13
jne 0x2cb82f
leaq 0xa31dc(%rip), %rax     # 0x36e9dd
leaq 0x70(%rsp), %rdx
movq %rax, (%rdx)
movl $0x1, 0x8(%rdx)
movq %r13, %rdi
movl $0x98, %esi
xorl %ecx, %ecx
callq 0x2b1c27
movq (%rbx), %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x2b1a3f
movq %rax, %r15
subq $0x8, %rsp
movq %rbx, %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0x40(%rsp), %r8
movq 0x38(%rsp), %r9
pushq $0x0
pushq $0x0
pushq 0x38(%rsp)
callq 0x2b18d8
addq $0x20, %rsp
movq %rax, %r15
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2b1dc0
movq 0x28(%rsp), %rbp
movq %rax, (%rbp)
testq %rax, %rax
je 0x2cb947
movq %r15, 0x30(%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2c8465
orb $0x40, 0x4(%r15)
movq %rbx, %rdi
movq %r15, %rsi
movl $0x40, %edx
callq 0x2c4234
movq %rax, %r12
movl 0x1c(%rsp), %eax
andl $0x8, %eax
orl %eax, 0x4(%r15)
testq %r12, %r12
je 0x2cb95e
movl $0x10, %ecx
movq (%rsp), %rdx
movq %rdx, %rdi
movq %r12, %rsi
rep  movsq (%rsi), %es:(%rdi)
orb $0x2, 0x40(%rdx)
movq (%rbp), %rax
movq %rdx, 0x28(%rax)
xorps %xmm0, %xmm0
leaq 0xa0(%rsp), %rdi
movaps %xmm0, (%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, 0x20(%rdi)
leaq 0x6f89(%rip), %rax      # 0x2d2883
movq %rax, 0x8(%rdi)
leaq 0x6f94(%rip), %rax      # 0x2d2899
movq %rax, 0x10(%rdi)
leaq 0x6f8f(%rip), %rax      # 0x2d289f
movq %rax, 0x18(%rdi)
movq %r15, %rsi
callq 0x2c0b5b
xorl %r14d, %r14d
movq %r12, %rsi
jmp 0x2cb968
testq %r14, %r14
je 0x2cb93f
movq 0x130(%r14), %rax
testq %rax, %rax
je 0x2cb93f
movl $0x7, 0x18(%rax)
incl 0x30(%rax)
movl $0x7, %r15d
jmp 0x2cb9ad
testq %r15, %r15
je 0x2cb964
movq %r13, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x2a62e6
jmp 0x2cb964
movl $0x7, %r14d
movq (%rsp), %rsi
cmpb $0x0, 0x61(%r13)
movl $0x7, %r15d
cmovel %r14d, %r15d
movq %r13, %rdi
callq 0x282904
testl %r15d, %r15d
je 0x2cb9aa
cmpl $0x0, 0x30(%rbx)
jne 0x2cb9ad
movq (%rbx), %rax
testq %rax, %rax
je 0x2cb9ad
movq 0x130(%rax), %rax
testq %rax, %rax
je 0x2cb9ad
movl $0x7, 0x18(%rax)
incl 0x30(%rax)
jmp 0x2cb9ad
xorl %r15d, %r15d
movl %r15d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq