// File: /workspace/github_c_makefile_star2_O1/ukontainer[P]sqlite-bench/sqlite3.c
// Function: sqlite3WindowRewrite
SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
  int rc = SQLITE_OK;
  if( p->pWin ){
    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 */

    p->pSrc = 0;
    p->pWhere = 0;
    p->pGroupBy = 0;
    p->pHaving = 0;

    /* 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);
    if( pSort && p->pOrderBy ){
      if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
        sqlite3ExprListDelete(db, p->pOrderBy);
        p->pOrderBy = 0;
      }
    }

    /* 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++;

    selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, &pSublist);
    selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, &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);
    pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy);

    /* 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){
      pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
      pSublist = exprListAppendList(pParse, pSublist, pWin->pOwner->x.pList);
      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, 
          sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0)
      );
    }

    pSub = sqlite3SelectNew(
        pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
    );
    p->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
    assert( p->pSrc || db->mallocFailed );
    if( p->pSrc ){
      p->pSrc->a[0].pSelect = pSub;
      sqlite3SrcListAssignCursors(pParse, p->pSrc);
      if( sqlite3ExpandSubquery(pParse, &p->pSrc->a[0]) ){
        rc = SQLITE_NOMEM;
      }else{
        pSub->selFlags |= SF_Expanded;
        p->selFlags &= ~SF_Aggregate;
        sqlite3SelectPrep(pParse, pSub, 0);
      }

      sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, pSublist->nExpr);
    }else{
      sqlite3SelectDelete(db, pSub);
    }
    if( db->mallocFailed ) rc = SQLITE_NOMEM;
  }

  return rc;
}

// Assembly:
cmpq $0x0, 0x70(%rsi)
je 0x45b584
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %r12
movq %rdi, %r15
callq 0x44e8aa
movq %rax, %r14
movq (%r15), %rbx
movq 0x28(%r12), %rax
movq %rax, 0x28(%rsp)
movq 0x30(%r12), %rax
movq %rax, 0x48(%rsp)
movq 0x38(%r12), %rax
movq %rax, 0x40(%rsp)
movq 0x40(%r12), %rax
movq %rax, 0x38(%rsp)
movq 0x70(%r12), %r13
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%r12)
movups %xmm0, 0x28(%r12)
movq 0x8(%r13), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x453664
movq 0x10(%r13), %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0x461d83
movq %rax, %rbp
testq %rax, %rax
je 0x45b3cd
movq 0x48(%r12), %rsi
testq %rsi, %rsi
je 0x45b3cd
movq %rbp, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x452f83
testl %eax, %eax
jne 0x45b3cd
movq 0x48(%r12), %rsi
testq %rsi, %rsi
je 0x45b3c4
movq %rbx, %rdi
callq 0x439a79
movq $0x0, 0x48(%r12)
movq %rbp, 0x30(%rsp)
movq %r14, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movl 0x34(%r15), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x34(%r15)
movl %eax, 0x48(%r13)
movq (%r12), %rsi
leaq 0x50(%rsp), %rbp
xorps %xmm0, %xmm0
movaps %xmm0, (%rbp)
movaps %xmm0, 0x10(%rbp)
movaps %xmm0, 0x20(%rbp)
movq %r12, %rbx
movq %r13, %r12
leaq 0x80(%rsp), %r13
movaps %xmm0, (%r13)
movaps %xmm0, 0x10(%r13)
movq %r12, (%r13)
movq 0x28(%rsp), %r14
movq %r14, 0x8(%r13)
movq %r15, (%rbp)
leaq 0x69e6(%rip), %rax      # 0x461e19
movq %rax, 0x8(%rbp)
leaq 0x6b04(%rip), %rax      # 0x461f42
movq %rax, 0x10(%rbp)
movq %r13, 0x28(%rbp)
movq %rbp, %rdi
callq 0x452c00
movq 0x10(%r13), %rax
movq %rbx, 0x8(%rsp)
movq 0x48(%rbx), %rsi
xorps %xmm0, %xmm0
movaps %xmm0, (%rbp)
movaps %xmm0, 0x10(%rbp)
movaps %xmm0, 0x20(%rbp)
movaps %xmm0, 0x10(%r13)
movaps %xmm0, (%r13)
movq %rax, 0x10(%r13)
movq %r12, 0x20(%rsp)
movq %r12, (%r13)
movq %r14, 0x8(%r13)
movq %r15, (%rbp)
leaq 0x6989(%rip), %rax      # 0x461e19
movq %rax, 0x8(%rbp)
leaq 0x6aa7(%rip), %rax      # 0x461f42
movq %rax, 0x10(%rbp)
movq %r13, 0x28(%rbp)
leaq 0x50(%rsp), %rdi
callq 0x452c00
movq 0x10(%r13), %rsi
testq %rsi, %rsi
movl $0x0, %eax
je 0x45b4bd
movl (%rsi), %eax
movq 0x20(%rsp), %r13
movl %eax, 0x68(%r13)
movq 0x8(%r13), %rdx
movq %r15, %rdi
callq 0x461d83
movq 0x10(%r13), %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0x461d83
movq %rax, %rbp
testq %r13, %r13
je 0x45b587
movq 0x8(%rsp), %r12
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %r14
movl $0x0, %eax
testq %rbp, %rbp
je 0x45b509
movl (%rbp), %eax
movl %eax, 0x6c(%r13)
movq 0x60(%r13), %rax
movq 0x20(%rax), %rdx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x461d83
movq %rax, %rbp
movq 0x38(%r13), %rsi
testq %rsi, %rsi
je 0x45b549
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x453111
movq %r15, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x444ece
movq %rax, %rbp
movl 0x38(%r15), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x38(%r15)
movl %ecx, 0x4c(%r13)
addl $0x2, %eax
movl %eax, 0x38(%r15)
movl %eax, 0x50(%r13)
movq %r14, %rdi
movl $0x49, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x43bae0
movq 0x30(%r13), %r13
testq %r13, %r13
jne 0x45b4fc
jmp 0x45b596
xorl %eax, %eax
retq
movq 0x8(%rsp), %r12
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %r14
addq $0x28, %r12
testq %rbp, %rbp
jne 0x45b5c5
leaq 0x142dea(%rip), %rdx    # 0x59e390
movq %rbx, %rdi
movl $0x8f, %esi
xorl %ecx, %ecx
callq 0x445107
movq %r15, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x444ece
movq %rax, %rbp
subq $0x8, %rsp
movq %r15, %rdi
movq %rbp, %rsi
movq 0x30(%rsp), %rdx
movq 0x50(%rsp), %rcx
movq 0x48(%rsp), %r8
movq 0x40(%rsp), %r9
pushq $0x0
pushq $0x0
pushq 0x48(%rsp)
callq 0x444d6b
addq $0x20, %rsp
movq %rax, %r13
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x445281
movq %rax, (%r12)
testq %rax, %rax
je 0x45b672
movq %r13, 0x30(%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x4589d1
movq (%r12), %rsi
addq $0x8, %rsi
movq %r15, %rdi
callq 0x458a2c
movl $0x7, %r12d
testl %eax, %eax
jne 0x45b655
orb $0x40, 0xc(%r13)
movq 0x8(%rsp), %rax
andb $-0x9, 0xc(%rax)
xorl %r12d, %r12d
movq %r15, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x4571f1
movq 0x20(%rsp), %rax
movl 0x48(%rax), %edx
movl (%rbp), %ecx
movq %r14, %rdi
movl $0x71, %esi
xorl %r8d, %r8d
callq 0x43bae0
jmp 0x45b68a
testq %r13, %r13
je 0x45b687
movq %rbx, %rdi
movq %r13, %rsi
movl $0x1, %edx
callq 0x4397c2
xorl %r12d, %r12d
cmpb $0x0, 0x61(%rbx)
movl $0x7, %eax
cmovel %r12d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq