// File: /workspace/github_c_makefile_star2_O1/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
movq %rdi, %rbx
xorl %r12d, %r12d
cmpq $0x0, 0x70(%rsi)
je 0x2d52db
movq %rsi, %rbp
cmpq $0x0, 0x50(%rsi)
jne 0x2d52db
testb $0x10, 0x6(%rbp)
jne 0x2d52db
movq %rbx, %rdi
callq 0x2c5ddf
movq %rax, %r13
movq (%rbx), %r15
movq 0x28(%rbp), %rax
movq %rax, 0x10(%rsp)
movq 0x30(%rbp), %rax
movq %rax, 0x68(%rsp)
movq 0x38(%rbp), %rax
movq %rax, 0x60(%rsp)
movq 0x40(%rbp), %rax
movq %rax, 0x58(%rsp)
movq 0x70(%rbp), %r12
movl 0x4(%rbp), %r14d
movl $0x80, %esi
movq %r15, %rdi
callq 0x283569
movq %rax, (%rsp)
testq %rax, %rax
je 0x2d513f
movl %r14d, 0x1c(%rsp)
leaq 0x28(%rbp), %r14
xorps %xmm0, %xmm0
leaq 0xa0(%rsp), %rdi
movaps %xmm0, (%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, 0x20(%rdi)
movq %rbx, (%rdi)
leaq 0x7276(%rip), %rax      # 0x2dc095
movq %rax, 0x8(%rdi)
leaq -0x20c6(%rip), %rax     # 0x2d2d64
movq %rax, 0x10(%rdi)
movq %rbp, %rsi
callq 0x2ca2ea
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movq %r14, 0x50(%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 0x2dbf4b
movq 0x18(%r12), %rdx
movq %rbx, %rdi
movq %rax, %rsi
movl $0x1, %ecx
callq 0x2dbf4b
testq %rax, %rax
je 0x2d4ed3
movq 0x48(%rbp), %rsi
testq %rsi, %rsi
je 0x2d4ed3
movl (%rsi), %ecx
movl (%rax), %edx
cmpl %edx, %ecx
jg 0x2d4ed3
movl %edx, 0x8(%rsp)
movl %ecx, (%rax)
movq %rax, %r14
movq %rax, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x2ca9c8
testl %eax, %eax
jne 0x2d4ec9
movq 0x48(%rbp), %rsi
testq %rsi, %rsi
je 0x2d4ec1
movq %r15, %rdi
callq 0x2b0183
movq $0x0, 0x48(%rbp)
movl 0x8(%rsp), %eax
movl %eax, (%r14)
movq %r14, %rax
movq %rax, 0x48(%rsp)
movq %r15, 0x8(%rsp)
movl 0x34(%rbx), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x34(%rbx)
movl %eax, 0x58(%r12)
addl $0x4, %eax
movl %eax, 0x34(%rbx)
movq 0x20(%rbp), %rsi
leaq 0x70(%rsp), %r14
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
movaps %xmm0, 0x10(%r14)
movaps %xmm0, 0x20(%r14)
leaq 0x20(%rsp), %r15
movaps %xmm0, (%r15)
movaps %xmm0, 0x10(%r15)
movl $0x0, %eax
movq %rax, 0x20(%r15)
movq %r12, (%r15)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%r15)
movq (%rsp), %rax
movq %rax, 0x18(%r15)
movq %rbx, (%r14)
leaq 0x71e4(%rip), %rax      # 0x2dc124
movq %rax, 0x8(%r14)
leaq 0x73aa(%rip), %rax      # 0x2dc2f5
movq %rax, 0x10(%r14)
movq %r15, 0x28(%r14)
movq %r14, %rdi
callq 0x2ca44f
movq 0x10(%r15), %rax
movq 0x48(%rbp), %rsi
xorl %ebp, %ebp
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
movaps %xmm0, 0x10(%r14)
movaps %xmm0, 0x20(%r14)
movaps %xmm0, 0x10(%r15)
movaps %xmm0, (%r15)
movq %rbp, 0x20(%r15)
movq %rax, 0x10(%r15)
movq %r12, (%r15)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%r15)
movq (%rsp), %rax
movq %rax, 0x18(%r15)
movq %rbx, (%r14)
leaq 0x717f(%rip), %rax      # 0x2dc124
movq %rax, 0x8(%r14)
leaq 0x7345(%rip), %rax      # 0x2dc2f5
movq %rax, 0x10(%r14)
movq %r15, 0x28(%r14)
leaq 0x70(%rsp), %rdi
callq 0x2ca44f
movq 0x10(%r15), %rsi
testq %rsi, %rsi
je 0x2d4fcd
movl (%rsi), %ebp
movl %ebp, 0x78(%r12)
movq 0x10(%r12), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x2dbf4b
movq 0x18(%r12), %rdx
movq %rbx, %rdi
movq %rax, %rsi
xorl %ecx, %ecx
callq 0x2dbf4b
movq %rax, %rbp
testq %r12, %r12
je 0x2d516a
movq %r12, %r14
movq 0x8(%rsp), %r15
movq 0x50(%r14), %rax
movq 0x70(%r14), %rcx
movq 0x20(%rcx), %rdx
testb $0x10, 0x6(%rax)
jne 0x2d502a
testq %rbp, %rbp
je 0x2d50bb
movl (%rbp), %eax
jmp 0x2d50bd
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x40(%rsp)
movq %rbp, 0x30(%rsp)
movq %r12, 0x20(%rsp)
movq 0x10(%rsp), %rax
movq %rax, 0x28(%rsp)
movq (%rsp), %rax
movq %rax, 0x38(%rsp)
movq %rbx, 0x70(%rsp)
leaq 0x70a6(%rip), %rax      # 0x2dc124
movq %rax, 0x78(%rsp)
leaq 0x726b(%rip), %rax      # 0x2dc2f5
movq %rax, 0x80(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, 0x98(%rsp)
leaq 0x70(%rsp), %rdi
movq %rdx, %rsi
callq 0x2ca44f
movq 0x30(%rsp), %rbp
testq %rbp, %rbp
je 0x2d50d3
movl (%rbp), %eax
jmp 0x2d50d5
xorl %eax, %eax
movl %eax, 0x7c(%r14)
movq %rbx, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x2dbf4b
movq %rax, %rbp
jmp 0x2d50e1
xorl %eax, %eax
movl %eax, 0x7c(%r14)
movb $0x1, 0x8c(%r14)
movq 0x48(%r14), %rsi
testq %rsi, %rsi
je 0x2d5107
movq %r15, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2cbf97
movq %rbx, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x2bb1a3
movq %rax, %rbp
movl 0x38(%rbx), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x38(%rbx)
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 0x2b200c
movq 0x40(%r14), %r14
testq %r14, %r14
jne 0x2d5007
jmp 0x2d516f
xorl %r12d, %r12d
testq %r15, %r15
je 0x2d52d4
movq 0x130(%r15), %rax
testq %rax, %rax
je 0x2d52d4
movl $0x7, 0x18(%rax)
incl 0x30(%rax)
jmp 0x2d52d4
movq 0x8(%rsp), %r15
testq %rbp, %rbp
jne 0x2d51a9
leaq 0xa2ad2(%rip), %rax     # 0x377c4d
leaq 0x70(%rsp), %rdx
movq %rax, (%rdx)
movl $0x1, 0x8(%rdx)
movq %r15, %rdi
movl $0x98, %esi
xorl %ecx, %ecx
callq 0x2bb38d
movq %rbx, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x2bb1a3
movq %rax, %rbp
subq $0x8, %rsp
movq %rbx, %rdi
movq %rbp, %rsi
movq 0x18(%rsp), %rdx
movq 0x70(%rsp), %rcx
movq 0x68(%rsp), %r8
movq 0x60(%rsp), %r9
pushq $0x0
pushq $0x0
pushq 0x60(%rsp)
callq 0x2bb03c
addq $0x20, %rsp
movq %rax, %r12
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2bb525
movq 0x50(%rsp), %rbp
movq %rax, (%rbp)
testq %rax, %rax
je 0x2d5299
movq %r12, 0x30(%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2d1c8a
orb $0x40, 0x4(%r12)
movq %rbx, %rdi
movq %r12, %rsi
movl $0x40, %edx
callq 0x2cd980
movq %rax, %r13
movl 0x1c(%rsp), %eax
andl $0x8, %eax
orl %eax, 0x4(%r12)
testq %r13, %r13
je 0x2d52b0
movl $0x10, %ecx
movq (%rsp), %rdx
movq %rdx, %rdi
movq %r13, %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 0x6e03(%rip), %rax      # 0x2dc075
movq %rax, 0x8(%rdi)
leaq 0x6e0e(%rip), %rax      # 0x2dc08b
movq %rax, 0x10(%rdi)
leaq 0x6e09(%rip), %rax      # 0x2dc091
movq %rax, 0x18(%rdi)
movq %r12, %rsi
callq 0x2ca2ea
xorl %r14d, %r14d
jmp 0x2d52ba
testq %r12, %r12
je 0x2d52b6
movq %r15, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0x2afecb
jmp 0x2d52b6
movl $0x7, %r14d
movq (%rsp), %r13
cmpb $0x0, 0x61(%r15)
movl $0x7, %r12d
cmovel %r14d, %r12d
movq %r15, %rdi
movq %r13, %rsi
callq 0x28c3e7
cmpq $0x0, (%rsp)
je 0x2d531b
testl %r12d, %r12d
je 0x2d52e6
cmpl $0x0, 0x30(%rbx)
je 0x2d52fb
movl %r12d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rax
testq %rax, %rax
je 0x2d52e6
movq 0x130(%rax), %rax
testq %rax, %rax
je 0x2d52e6
movl $0x7, 0x18(%rax)
incl 0x30(%rax)
jmp 0x2d52e6
movl $0x7, %r12d
jmp 0x2d52e6