// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x70(%rax)
je 0x47e178
movq -0x8(%rbp), %rdi
callq 0x46bb00
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x40(%rax)
movq -0x28(%rbp), %rdi
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rsi
xorl %edx, %edx
callq 0x472350
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x68(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x483420
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x47dea1
movq -0x10(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x47dea1
movq -0x58(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x471900
cmpl $0x0, %eax
jne 0x47de9f
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rsi
callq 0x44fde0
movq -0x10(%rbp), %rax
movq $0x0, 0x48(%rax)
jmp 0x47dea1
movq -0x8(%rbp), %rax
movl 0x34(%rax), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x34(%rax)
movq -0x68(%rbp), %rax
movl %ecx, 0x48(%rax)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x10(%rbp), %rax
movq (%rax), %rcx
leaq -0x60(%rbp), %r8
callq 0x483510
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rcx
leaq -0x60(%rbp), %r8
callq 0x483510
cmpq $0x0, -0x60(%rbp)
je 0x47df02
movq -0x60(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x7c(%rbp)
jmp 0x47df09
xorl %eax, %eax
movl %eax, -0x7c(%rbp)
jmp 0x47df09
movl -0x7c(%rbp), %ecx
movq -0x68(%rbp), %rax
movl %ecx, 0x68(%rax)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0x483420
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x483420
movq %rax, -0x60(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
je 0x47e01f
cmpq $0x0, -0x60(%rbp)
je 0x47df6a
movq -0x60(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x80(%rbp)
jmp 0x47df71
xorl %eax, %eax
movl %eax, -0x80(%rbp)
jmp 0x47df71
movl -0x80(%rbp), %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x6c(%rax)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x70(%rbp), %rax
movq 0x60(%rax), %rax
movq 0x20(%rax), %rdx
callq 0x483420
movq %rax, -0x60(%rbp)
movq -0x70(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x47dfcf
movq -0x28(%rbp), %rdi
movq -0x70(%rbp), %rax
movq 0x38(%rax), %rsi
xorl %edx, %edx
callq 0x4717d0
movq %rax, -0x78(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x78(%rbp), %rdx
callq 0x460810
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rax
movl 0x38(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x38(%rax)
movq -0x70(%rbp), %rax
movl %ecx, 0x4c(%rax)
movq -0x8(%rbp), %rax
movl 0x38(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x38(%rax)
movq -0x70(%rbp), %rax
movl %ecx, 0x50(%rax)
movq -0x20(%rbp), %rdi
movq -0x70(%rbp), %rax
movl 0x4c(%rax), %ecx
movl $0x49, %esi
xorl %edx, %edx
callq 0x453790
movq -0x70(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x70(%rbp)
jmp 0x47df4d
cmpq $0x0, -0x60(%rbp)
jne 0x47e05f
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x28(%rbp), %rdi
movl $0x8f, %esi
leaq 0x14e3df(%rip), %rdx    # 0x5cc420
xorl %ecx, %ecx
callq 0x460aa0
movq -0x88(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
movl %eax, %esi
callq 0x460810
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %r8
movq -0x50(%rbp), %r9
movq -0x58(%rbp), %rax
xorl %r10d, %r10d
xorl %r10d, %r10d
movq %rax, (%rsp)
movl $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
callq 0x460620
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x460db0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x47e158
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rsi
callq 0x4797f0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rsi
addq $0x8, %rsi
callq 0x479ce0
cmpl $0x0, %eax
je 0x47e110
movl $0x7, -0x14(%rbp)
jmp 0x47e13b
movq -0x30(%rbp), %rax
movl 0xc(%rax), %ecx
orl $0x40, %ecx
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %ecx
andl $-0x9, %ecx
movl %ecx, 0xc(%rax)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x478170
movq -0x20(%rbp), %rdi
movq -0x68(%rbp), %rax
movl 0x48(%rax), %edx
movq -0x60(%rbp), %rax
movl (%rax), %ecx
movl $0x71, %esi
callq 0x453790
jmp 0x47e165
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x44ff20
movq -0x28(%rbp), %rax
cmpb $0x0, 0x61(%rax)
je 0x47e176
movl $0x7, -0x14(%rbp)
jmp 0x47e178
movl -0x14(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)