// File: /workspace/github_c_makefile_star2_O3/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 0x45a7b4
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r12
movq %rdi, %r15
callq 0x44dbe5
movq %rax, %r14
movq (%r15), %rbx
movq 0x28(%r12), %rax
movq %rax, 0x10(%rsp)
movq 0x30(%r12), %rax
movq %rax, 0x38(%rsp)
movq 0x38(%r12), %rax
movq %rax, 0x30(%rsp)
movq 0x40(%r12), %rax
movq %rax, 0x28(%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 0x4528d0
movq %r13, 0x8(%rsp)
movq 0x10(%r13), %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0x4610c0
movq %rax, %r13
testq %rax, %rax
je 0x45a559
movq 0x48(%r12), %rsi
testq %rsi, %rsi
je 0x45a559
movq %r13, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x4521ef
testl %eax, %eax
jne 0x45a559
movq 0x48(%r12), %rsi
testq %rsi, %rsi
je 0x45a550
movq %rbx, %rdi
callq 0x4385eb
movq $0x0, 0x48(%r12)
movq %r13, 0x20(%rsp)
movq %rbx, 0x40(%rsp)
movl 0x34(%r15), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x34(%r15)
movq 0x8(%rsp), %rbx
movl %eax, 0x48(%rbx)
movq (%r12), %rsi
leaq 0x48(%rsp), %rbp
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rbp)
leaq 0x78(%rsp), %r13
movups %xmm0, 0x10(%r13)
movq %rbx, (%r13)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%r13)
movq %r15, (%rbp)
leaq 0x6bae(%rip), %rax      # 0x461156
movq %rax, 0x8(%rbp)
leaq 0x6cb9(%rip), %rax      # 0x46126c
movq %rax, 0x10(%rbp)
movq %r13, 0x28(%rbp)
movq %rbp, %rdi
callq 0x451e70
movq 0x10(%r13), %rax
movq %r12, 0x18(%rsp)
movq 0x48(%r12), %rsi
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rbp)
movq $0x0, 0x18(%r13)
movq %rax, 0x10(%r13)
movq %rbx, (%r13)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%r13)
movq %r15, (%rbp)
leaq 0x6b5a(%rip), %rax      # 0x461156
movq %rax, 0x8(%rbp)
leaq 0x6c65(%rip), %rax      # 0x46126c
movq %rax, 0x10(%rbp)
movq %r13, 0x28(%rbp)
leaq 0x48(%rsp), %rdi
callq 0x451e70
movq 0x10(%r13), %rsi
testq %rsi, %rsi
movl $0x0, %ecx
je 0x45a629
movl (%rsi), %ecx
movq 0x18(%rsp), %rax
leaq 0x28(%rax), %r12
movq 0x8(%rsp), %r13
movl %ecx, 0x68(%r13)
movq 0x8(%r13), %rdx
movq %r15, %rdi
callq 0x4610c0
movq 0x10(%r13), %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0x4610c0
movq %rax, %rbp
movq 0x40(%rsp), %rbx
movl $0x0, %eax
testq %rbp, %rbp
je 0x45a66b
movl (%rbp), %eax
movl %eax, 0x6c(%r13)
movq 0x60(%r13), %rax
movq 0x20(%rax), %rdx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x4610c0
movq %rax, %rbp
movq 0x38(%r13), %rsi
testq %rsi, %rsi
je 0x45a6ab
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x452376
movq (%r15), %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x444182
movq %rax, %rbp
movl 0x38(%r15), %eax
leal 0x1(%rax), %ecx
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 0x43a65f
movq 0x30(%r13), %r13
testq %r13, %r13
jne 0x45a65e
testq %rbp, %rbp
jne 0x45a707
leaq 0x143ca8(%rip), %rdx    # 0x59e390
movq %rbx, %rdi
movl $0x8f, %esi
xorl %ecx, %ecx
callq 0x4443be
movq (%r15), %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x444182
movq %rax, %rbp
subq $0x8, %rsp
movq %r15, %rdi
movq %rbp, %rsi
movq 0x18(%rsp), %rdx
movq 0x40(%rsp), %rcx
movq 0x38(%rsp), %r8
movq 0x30(%rsp), %r9
pushq $0x0
pushq $0x0
pushq 0x38(%rsp)
callq 0x44401f
addq $0x20, %rsp
movq %rax, %r13
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x44453c
movq %rax, (%r12)
testq %rax, %rax
je 0x45a7b7
movq %r13, 0x30(%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x457b87
movq (%r12), %rsi
addq $0x8, %rsi
movq %r15, %rdi
callq 0x457be5
movl $0x7, %r12d
testl %eax, %eax
jne 0x45a797
orb $0x40, 0xc(%r13)
movq 0x18(%rsp), %rax
andb $-0x9, 0xc(%rax)
xorl %r12d, %r12d
movq %r15, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x456573
movq 0x8(%rsp), %rax
movl 0x48(%rax), %edx
movl (%rbp), %ecx
movq %r14, %rdi
movl $0x71, %esi
xorl %r8d, %r8d
callq 0x43a65f
jmp 0x45a7cf
xorl %eax, %eax
retq
testq %r13, %r13
je 0x45a7cc
movq %rbx, %rdi
movq %r13, %rsi
movl $0x1, %edx
callq 0x43832f
xorl %r12d, %r12d
cmpb $0x0, 0x61(%rbx)
movl $0x7, %eax
cmovel %r12d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq