// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x70(%rax)
je 0x878c3
movq -0x18(%rbp), %rax
cmpq $0x0, 0x50(%rax)
jne 0x878c3
movq -0x10(%rbp), %rdi
callq 0x73cc0
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x30(%rbp), %rdi
movl $0x80, %esi
callq 0x148f0
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
jne 0x873b9
movl $0x7, -0x4(%rbp)
jmp 0x878c9
movq -0x18(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x18(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x18(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x18(%rbp), %rax
movq $0x0, 0x40(%rax)
movq -0x18(%rbp), %rax
movl 0xc(%rax), %ecx
andl $-0x9, %ecx
movl %ecx, 0xc(%rax)
movq -0x30(%rbp), %rdi
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rsi
xorl %edx, %edx
callq 0x7bb50
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rdx
movl $0x1, %ecx
callq 0x8db80
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x874af
movq -0x18(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x874af
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movl (%rax), %eax
movq -0x60(%rbp), %rcx
cmpl (%rcx), %eax
jg 0x874af
movq -0x60(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x84(%rbp)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movl (%rax), %ecx
movq -0x60(%rbp), %rax
movl %ecx, (%rax)
movq -0x60(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x79c50
cmpl $0x0, %eax
jne 0x874a3
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rsi
callq 0x57290
movq -0x18(%rbp), %rax
movq $0x0, 0x48(%rax)
movl -0x84(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movl 0x34(%rax), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x34(%rax)
movq -0x70(%rbp), %rax
movl %ecx, 0x58(%rax)
movq -0x10(%rbp), %rax
movl 0x34(%rax), %ecx
addl $0x3, %ecx
movl %ecx, 0x34(%rax)
movq -0x10(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x18(%rbp), %rax
movq (%rax), %rcx
movq -0x80(%rbp), %r8
leaq -0x68(%rbp), %r9
callq 0x8dca0
movq -0x10(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rcx
movq -0x80(%rbp), %r8
leaq -0x68(%rbp), %r9
callq 0x8dca0
cmpq $0x0, -0x68(%rbp)
je 0x87528
movq -0x68(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xa4(%rbp)
jmp 0x87532
xorl %eax, %eax
movl %eax, -0xa4(%rbp)
jmp 0x87532
movl -0xa4(%rbp), %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x78(%rax)
movq -0x10(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rdx
xorl %ecx, %ecx
callq 0x8db80
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rdx
xorl %ecx, %ecx
callq 0x8db80
movq %rax, -0x68(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
je 0x876dc
movq -0x78(%rbp), %rax
movq 0x70(%rax), %rax
movq 0x20(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x78(%rbp), %rax
movq 0x50(%rax), %rax
movl 0x4(%rax), %eax
andl $0x100000, %eax         # imm = 0x100000
cmpl $0x0, %eax
je 0x87609
movq -0x10(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x90(%rbp), %rcx
movq -0x80(%rbp), %r8
leaq -0x68(%rbp), %r9
callq 0x8dca0
cmpq $0x0, -0x68(%rbp)
je 0x875e5
movq -0x68(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xa8(%rbp)
jmp 0x875ef
xorl %eax, %eax
movl %eax, -0xa8(%rbp)
jmp 0x875ef
movl -0xa8(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x7c(%rax)
movq -0x78(%rbp), %rax
movb $0x1, 0x8c(%rax)
jmp 0x8764f
cmpq $0x0, -0x68(%rbp)
je 0x8761e
movq -0x68(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xac(%rbp)
jmp 0x87628
xorl %eax, %eax
movl %eax, -0xac(%rbp)
jmp 0x87628
movl -0xac(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x7c(%rax)
movq -0x10(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x90(%rbp), %rdx
xorl %ecx, %ecx
callq 0x8db80
movq %rax, -0x68(%rbp)
movq -0x78(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x8768c
movq -0x30(%rbp), %rdi
movq -0x78(%rbp), %rax
movq 0x48(%rax), %rsi
xorl %edx, %edx
callq 0x77a40
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x98(%rbp), %rdx
callq 0x681a0
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movl 0x38(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x38(%rax)
movq -0x78(%rbp), %rax
movl %ecx, 0x5c(%rax)
movq -0x10(%rbp), %rax
movl 0x38(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x38(%rax)
movq -0x78(%rbp), %rax
movl %ecx, 0x60(%rax)
movq -0x28(%rbp), %rdi
movq -0x78(%rbp), %rax
movl 0x5c(%rax), %ecx
movl $0x49, %esi
xorl %edx, %edx
callq 0x5ad50
movq -0x78(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x78(%rbp)
jmp 0x8757d
cmpq $0x0, -0x68(%rbp)
jne 0x8771a
movq -0x10(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x30(%rbp), %rdi
movl $0x96, %esi
leaq 0x37b04(%rip), %rdx     # 0xbf202
callq 0x683f0
movq -0xb8(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
movl %eax, %esi
callq 0x681a0
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x50(%rbp), %r8
movq -0x58(%rbp), %r9
movq -0x60(%rbp), %rax
xorl %r10d, %r10d
xorl %r10d, %r10d
movq %rax, (%rsp)
movl $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
callq 0x67fb0
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x68730
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x87898
movq -0x38(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rcx, 0x30(%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rsi
callq 0x829e0
movq -0x38(%rbp), %rax
movl 0xc(%rax), %ecx
orl $0x40, %ecx
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl $0x40, %edx
callq 0x7d920
movq %rax, -0xa0(%rbp)
cmpq $0x0, -0xa0(%rbp)
jne 0x877e1
movl $0x7, -0x1c(%rbp)
jmp 0x8781e
movq -0x80(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movl $0x80, %edx
callq 0x81d0
movq -0x80(%rbp), %rax
movl 0x40(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x40(%rax)
movq -0x80(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rcx, 0x28(%rax)
movq -0xa0(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x28(%rbp), %rdi
movq -0x70(%rbp), %rax
movl 0x58(%rax), %edx
movq -0x68(%rbp), %rax
movl (%rax), %ecx
movl $0x70, %esi
callq 0x5ad50
movq -0x28(%rbp), %rdi
movq -0x70(%rbp), %rax
movl 0x58(%rax), %edx
addl $0x1, %edx
movq -0x70(%rbp), %rax
movl 0x58(%rax), %ecx
movl $0x6d, %esi
callq 0x5ad50
movq -0x28(%rbp), %rdi
movq -0x70(%rbp), %rax
movl 0x58(%rax), %edx
addl $0x2, %edx
movq -0x70(%rbp), %rax
movl 0x58(%rax), %ecx
movl $0x6d, %esi
callq 0x5ad50
movq -0x28(%rbp), %rdi
movq -0x70(%rbp), %rax
movl 0x58(%rax), %edx
addl $0x3, %edx
movq -0x70(%rbp), %rax
movl 0x58(%rax), %ecx
movl $0x6d, %esi
callq 0x5ad50
jmp 0x878a5
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x573d0
movq -0x30(%rbp), %rax
cmpb $0x0, 0x61(%rax)
je 0x878b6
movl $0x7, -0x1c(%rbp)
movq -0x30(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0xb130
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)