// File: /workspace/github_c_makefile_star2_O3/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 0x62170
movq %rsi, %rbp
cmpq $0x0, 0x50(%rsi)
jne 0x62170
movq %rdi, %r12
callq 0x54856
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 0x11d1f
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x620cc
movq %r14, 0x38(%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 0x5a500
movq 0x18(%rbx), %rdx
movq %r12, %rdi
movq %rax, %rsi
movl $0x1, %ecx
callq 0x68c39
testq %rax, %rax
je 0x61db9
movq 0x48(%rbp), %rsi
testq %rsi, %rsi
je 0x61db9
movl (%rsi), %ecx
movl (%rax), %edx
cmpl %edx, %ecx
jg 0x61db9
movl %edx, 0x10(%rsp)
movl %ecx, (%rax)
movq %rax, %r14
movq %rax, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x58d87
testl %eax, %eax
jne 0x61daf
movq 0x48(%rbp), %rsi
testq %rsi, %rsi
je 0x61da7
movq %r13, %rdi
callq 0x3ebf2
movq $0x0, 0x48(%rbp)
movl 0x10(%rsp), %eax
movl %eax, (%r14)
movq %r14, %rax
movq %r15, 0x28(%rsp)
movq %rax, 0x30(%rsp)
movl 0x34(%r12), %eax
movl %eax, 0x58(%rbx)
addl $0x4, %eax
movl %eax, 0x34(%r12)
movq (%rbp), %rsi
leaq 0x78(%rsp), %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r14)
leaq 0x50(%rsp), %r15
xorl %eax, %eax
movq %rax, 0x20(%r15)
movq %rax, 0x10(%r15)
movq %rbx, (%r15)
movq 0x18(%rsp), %rax
movq %rax, 0x8(%r15)
movq 0x8(%rsp), %rax
movq %rax, 0x18(%r15)
movq %r12, (%r14)
leaq 0x6ee0(%rip), %rax      # 0x68cf2
movq %rax, 0x8(%r14)
leaq 0x7004(%rip), %rax      # 0x68e21
movq %rax, 0x10(%r14)
movq %r15, 0x28(%r14)
movq %r14, %rdi
callq 0x587d8
movq 0x10(%r15), %rax
movq 0x48(%rbp), %rsi
xorl %ebp, %ebp
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r14)
movq %rbp, 0x20(%r15)
movq %rax, 0x10(%r15)
movq %rbx, 0x10(%rsp)
movq %rbx, (%r15)
movq 0x18(%rsp), %rax
movq %rax, 0x8(%r15)
movq 0x8(%rsp), %rax
movq %rax, 0x18(%r15)
movq %r12, (%r14)
leaq 0x6e87(%rip), %rax      # 0x68cf2
movq %rax, 0x8(%r14)
leaq 0x6fab(%rip), %rax      # 0x68e21
movq %rax, 0x10(%r14)
movq %r15, 0x28(%r14)
leaq 0x78(%rsp), %rdi
callq 0x587d8
movq 0x10(%r15), %rsi
testq %rsi, %rsi
je 0x61e93
movl (%rsi), %ebp
movq 0x10(%rsp), %rbx
movl %ebp, 0x78(%rbx)
movq 0x10(%rbx), %rdx
movq %r12, %rdi
xorl %ecx, %ecx
callq 0x68c39
movq 0x18(%rbx), %rdx
movq %r12, %rdi
movq %rax, %rsi
xorl %ecx, %ecx
callq 0x68c39
movq %rax, %rbp
leaq 0x78(%rsp), %r14
movq 0x28(%rsp), %r15
movq 0x50(%rbx), %rax
movq 0x70(%rbx), %rcx
movq 0x20(%rcx), %rdx
testb $0x10, 0x6(%rax)
jne 0x61eea
testq %rbp, %rbp
je 0x61f6e
movl (%rbp), %eax
jmp 0x61f70
leaq 0x90(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x70(%rsp)
movq %rbp, 0x60(%rsp)
movq 0x10(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x68(%rsp)
movq %r12, 0x78(%rsp)
leaq 0x6dc2(%rip), %rax      # 0x68cf2
movq %rax, 0x80(%rsp)
leaq 0x6ee2(%rip), %rax      # 0x68e21
movq %rax, 0x88(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq %r14, %rdi
movq %rdx, %rsi
callq 0x587d8
movq 0x60(%rsp), %rbp
testq %rbp, %rbp
je 0x61f85
movl (%rbp), %eax
jmp 0x61f87
xorl %eax, %eax
movl %eax, 0x7c(%rbx)
movq %r12, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x68c39
movq %rax, %rbp
jmp 0x61f91
xorl %eax, %eax
movl %eax, 0x7c(%rbx)
movb $0x1, 0x8c(%rbx)
movq 0x48(%rbx), %rsi
testq %rsi, %rsi
je 0x61fb8
movq %r13, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x59f79
movq (%r12), %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x4a6a3
movq %rax, %rbp
movl 0x38(%r12), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x5c(%rbx)
addl $0x2, %eax
movl %eax, 0x38(%r12)
movl %eax, 0x60(%rbx)
movq %r15, %rdi
movl $0x49, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x40bac
movq 0x40(%rbx), %rbx
testq %rbx, %rbx
jne 0x61ec7
testq %rbp, %rbp
jne 0x62028
leaq 0x2c22a(%rip), %rax     # 0x8e223
leaq 0x78(%rsp), %rdx
movq %rax, (%rdx)
movl $0x1, 0x8(%rdx)
movq %r13, %rdi
movl $0x96, %esi
xorl %ecx, %ecx
callq 0x4a8df
movq (%r12), %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x4a6a3
movq %rax, %rbp
subq $0x8, %rsp
movq %r12, %rdi
movq %rbp, %rsi
movq 0x20(%rsp), %rdx
movq 0x40(%rsp), %rcx
movq 0x50(%rsp), %r8
movq 0x48(%rsp), %r9
pushq $0x0
pushq $0x0
pushq 0x48(%rsp)
callq 0x4a540
addq $0x20, %rsp
movq %rax, %r14
xorl %ebx, %ebx
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4aa78
movq 0x20(%rsp), %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x620d7
movq %r14, 0x30(%rax)
movq %r12, %rdi
movq %rax, %rsi
callq 0x5f2fb
orb $0x40, 0xc(%r14)
movq %r12, %rdi
movq %r14, %rsi
movl $0x40, %edx
callq 0x5b2e1
testq %rax, %rax
je 0x620ee
movl $0x10, %ecx
movq 0x8(%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 %ebx, %ebx
movq %rax, 0x8(%rsp)
jmp 0x620f3
movl $0x7, %r14d
jmp 0x62170
testq %r14, %r14
je 0x62154
movq %r13, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x3e936
jmp 0x62154
movl $0x7, %ebx
movq 0x10(%rsp), %r14
movl 0x58(%r14), %edx
movl (%rbp), %ecx
movq %r15, %rdi
movl $0x70, %esi
xorl %r8d, %r8d
callq 0x40bac
movl 0x58(%r14), %ecx
leal 0x1(%rcx), %edx
movq %r15, %rdi
movl $0x6d, %esi
xorl %r8d, %r8d
callq 0x40bac
movl 0x58(%r14), %ecx
leal 0x2(%rcx), %edx
movq %r15, %rdi
movl $0x6d, %esi
xorl %r8d, %r8d
callq 0x40bac
movl 0x58(%r14), %ecx
leal 0x3(%rcx), %edx
movq %r15, %rdi
movl $0x6d, %esi
xorl %r8d, %r8d
callq 0x40bac
cmpb $0x0, 0x61(%r13)
movl $0x7, %r14d
cmovel %ebx, %r14d
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x1b5f7
movl %r14d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq