// File: /workspace/github_c_makefile_star2_O1/jkbenaim[P]psfrag/sqlite3.c
// Function: sqlite3WindowRewrite
SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
  int rc = SQLITE_OK;
  if( p->pWin && p->pPrior==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;      /* Master window object */
    Window *pWin;                 /* Window object iterator */
    Table *pTab;

    pTab = sqlite3DbMallocZero(db, sizeof(Table));
    if( pTab==0 ){
      return SQLITE_NOMEM;
    }

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

    /* 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 = sqlite3ExprListDup(db, pMWin->pPartition, 0);
    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
    );
    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);
      if( pTab2==0 ){
        rc = SQLITE_NOMEM;
      }else{
        memcpy(pTab, pTab2, sizeof(Table));
        pTab->tabFlags |= TF_Ephemeral;
        p->pSrc->a[0].pTab = pTab;
        pTab = pTab2;
      }
      sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, pSublist->nExpr);
      sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+1, pMWin->iEphCsr);
      sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+2, pMWin->iEphCsr);
      sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+3, pMWin->iEphCsr);
    }else{
      sqlite3SelectDelete(db, pSub);
    }
    if( db->mallocFailed ) rc = SQLITE_NOMEM;
    sqlite3DbFree(db, pTab);
  }

  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
xorl %r14d, %r14d
cmpq $0x0, 0x70(%rsi)
je 0x629be
movq %rsi, %rbp
cmpq $0x0, 0x50(%rsi)
jne 0x629be
movq %rdi, %r12
callq 0x54e69
movq %rax, %r15
movq (%r12), %r13
movq 0x28(%rbp), %rax
movq %rax, 0x18(%rsp)
movq 0x30(%rbp), %r14
movq 0x38(%rbp), %rax
movq %rax, 0x48(%rsp)
movq 0x40(%rbp), %rax
movq %rax, 0x40(%rsp)
movq 0x70(%rbp), %rbx
movl $0x80, %esi
movq %r13, %rdi
callq 0x11de0
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x6291f
movq %r14, 0x30(%rsp)
leaq 0x28(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movq %rax, 0x20(%rsp)
movups %xmm0, (%rax)
andb $-0x9, 0xc(%rbp)
movq 0x10(%rbx), %rsi
movq %r13, %rdi
xorl %edx, %edx
callq 0x5aaa1
movq 0x18(%rbx), %rdx
movq %r12, %rdi
movq %rax, %rsi
movl $0x1, %ecx
callq 0x6916c
testq %rax, %rax
je 0x625b5
movq 0x48(%rbp), %rsi
testq %rsi, %rsi
je 0x625b5
movl (%rsi), %ecx
movl (%rax), %edx
cmpl %edx, %ecx
jg 0x625b5
movl %edx, 0x8(%rsp)
movl %ecx, (%rax)
movq %rax, %r14
movq %rax, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x59378
testl %eax, %eax
jne 0x625ab
movq 0x48(%rbp), %rsi
testq %rsi, %rsi
je 0x625a3
movq %r13, %rdi
callq 0x3f953
movq $0x0, 0x48(%rbp)
movl 0x8(%rsp), %eax
movl %eax, (%r14)
movq %r14, %rax
movq %rax, 0x28(%rsp)
movq %r15, 0x38(%rsp)
movq %r13, 0x8(%rsp)
movl 0x34(%r12), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x34(%r12)
movl %eax, 0x58(%rbx)
addl $0x4, %eax
movl %eax, 0x34(%r12)
movq (%rbp), %rsi
leaq 0x50(%rsp), %r14
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
movaps %xmm0, 0x10(%r14)
movaps %xmm0, 0x20(%r14)
leaq 0x80(%rsp), %r13
movaps %xmm0, (%r13)
movaps %xmm0, 0x10(%r13)
xorl %eax, %eax
movq %rax, 0x20(%r13)
movq %rbx, (%r13)
movq 0x18(%rsp), %rax
movq %rax, 0x8(%r13)
movq 0x10(%rsp), %r15
movq %r15, 0x18(%r13)
movq %r12, (%r14)
leaq 0x6c00(%rip), %rax      # 0x6922e
movq %rax, 0x8(%r14)
leaq 0x6d38(%rip), %rax      # 0x69371
movq %rax, 0x10(%r14)
movq %r13, 0x28(%r14)
movq %r14, %rdi
callq 0x58e20
movq 0x10(%r13), %rax
movq 0x48(%rbp), %rsi
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
movaps %xmm0, 0x10(%r14)
movaps %xmm0, 0x20(%r14)
movaps %xmm0, 0x10(%r13)
movaps %xmm0, (%r13)
xorl %ecx, %ecx
movq %rcx, 0x20(%r13)
movq %rax, 0x10(%r13)
movq %rbx, %rbp
movq %rbx, (%r13)
movq 0x18(%rsp), %rax
movq %rax, 0x8(%r13)
movq %r15, 0x18(%r13)
movq %r12, (%r14)
leaq 0x6b9a(%rip), %rax      # 0x6922e
movq %rax, 0x8(%r14)
leaq 0x6cd2(%rip), %rax      # 0x69371
movq %rax, 0x10(%r14)
movq %r13, 0x28(%r14)
leaq 0x50(%rsp), %rdi
callq 0x58e20
xorl %eax, %eax
movq 0x10(%r13), %rsi
testq %rsi, %rsi
je 0x626be
movl (%rsi), %eax
movq %rbp, %rbx
movl %eax, 0x78(%rbp)
movq 0x10(%rbp), %rdx
movq %r12, %rdi
xorl %ecx, %ecx
callq 0x6916c
movq 0x18(%rbp), %rdx
movq %r12, %rdi
movq %rax, %rsi
xorl %ecx, %ecx
callq 0x6916c
movq %rax, %rbp
testq %rbx, %rbx
movq 0x38(%rsp), %r15
je 0x6283d
movq %rbx, %r14
movq 0x50(%r14), %rax
movq 0x70(%r14), %rcx
movq 0x20(%rcx), %rdx
testb $0x10, 0x6(%rax)
jne 0x6271a
testq %rbp, %rbp
je 0x627b3
movl (%rbp), %eax
jmp 0x627b5
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movq $0x0, 0xa0(%rsp)
movq %rbp, 0x90(%rsp)
movq %rbx, 0x80(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x10(%rsp), %rax
movq %rax, 0x98(%rsp)
movq %r12, 0x50(%rsp)
leaq 0x6ab0(%rip), %rax      # 0x6922e
movq %rax, 0x58(%rsp)
leaq 0x6be7(%rip), %rax      # 0x69371
movq %rax, 0x60(%rsp)
movq %r13, 0x78(%rsp)
leaq 0x50(%rsp), %rdi
movq %rdx, %rsi
callq 0x58e20
movq 0x90(%rsp), %rbp
testq %rbp, %rbp
je 0x627cb
movl (%rbp), %eax
jmp 0x627cd
xorl %eax, %eax
movl %eax, 0x7c(%r14)
movq %r12, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x6916c
movq %rax, %rbp
jmp 0x627d9
xorl %eax, %eax
movl %eax, 0x7c(%r14)
movb $0x1, 0x8c(%r14)
movq 0x48(%r14), %rsi
testq %rsi, %rsi
je 0x62801
movq 0x8(%rsp), %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x5a513
movq %r12, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x4ad8b
movq %rax, %rbp
movl 0x38(%r12), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x38(%r12)
movl %ecx, 0x5c(%r14)
addl $0x2, %eax
movl %eax, 0x38(%r12)
movl %eax, 0x60(%r14)
movq %r15, %rdi
movl $0x49, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x41932
movq 0x40(%r14), %r14
testq %r14, %r14
jne 0x626f7
testq %rbp, %rbp
jne 0x62879
leaq 0x2c94e(%rip), %rax     # 0x8f197
leaq 0x50(%rsp), %rdx
movq %rax, (%rdx)
movl $0x1, 0x8(%rdx)
movq 0x8(%rsp), %rdi
movl $0x96, %esi
xorl %ecx, %ecx
callq 0x4afc4
movq %r12, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x4ad8b
movq %rax, %rbp
subq $0x8, %rsp
movq %r12, %rdi
movq %rbp, %rsi
movq 0x20(%rsp), %rdx
movq 0x38(%rsp), %rcx
movq 0x50(%rsp), %r8
movq 0x48(%rsp), %r9
pushq $0x0
pushq $0x0
pushq 0x40(%rsp)
callq 0x4ac28
addq $0x20, %rsp
movq %rax, %r14
xorl %r13d, %r13d
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4b15c
movq 0x20(%rsp), %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x6292a
movq %r14, 0x30(%rax)
movq %r12, %rdi
movq %rax, %rsi
callq 0x5fa55
orb $0x40, 0xc(%r14)
movq %r12, %rdi
movq %r14, %rsi
movl $0x40, %edx
callq 0x5b84a
testq %rax, %rax
je 0x62943
movl $0x10, %ecx
movq 0x10(%rsp), %rdx
movq %rdx, %rdi
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
orb $0x2, 0x40(%rdx)
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
movq %rdx, 0x28(%rcx)
xorl %r13d, %r13d
movq %rax, 0x10(%rsp)
jmp 0x62949
movl $0x7, %r14d
jmp 0x629be
testq %r14, %r14
je 0x629a1
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x3f69c
jmp 0x629a1
movl $0x7, %r13d
movl 0x58(%rbx), %edx
movl (%rbp), %ecx
movq %r15, %rdi
movl $0x70, %esi
xorl %r8d, %r8d
callq 0x41932
movl 0x58(%rbx), %ecx
leal 0x1(%rcx), %edx
movq %r15, %rdi
movl $0x6d, %esi
xorl %r8d, %r8d
callq 0x41932
movl 0x58(%rbx), %ecx
leal 0x2(%rcx), %edx
movq %r15, %rdi
movl $0x6d, %esi
xorl %r8d, %r8d
callq 0x41932
movl 0x58(%rbx), %ecx
leal 0x3(%rcx), %edx
movq %r15, %rdi
movl $0x6d, %esi
xorl %r8d, %r8d
callq 0x41932
movq 0x8(%rsp), %rdi
cmpb $0x0, 0x61(%rdi)
movl $0x7, %r14d
cmovel %r13d, %r14d
movq 0x10(%rsp), %rsi
callq 0x1b7c1
movl %r14d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq