// File: /workspace/github_c_makefile_star2_O0/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: sqlite3WindowRewrite
SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
  int rc = SQLITE_OK;
  if( p->pWin && p->pPrior==0 && (p->selFlags & SF_WinRewrite)==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;      /* Main window object */
    Window *pWin;                 /* Window object iterator */
    Table *pTab;
    Walker w;

    u32 selFlags = p->selFlags;

    pTab = sqlite3DbMallocZero(db, sizeof(Table));
    if( pTab==0 ){
      return sqlite3ErrorToParser(db, SQLITE_NOMEM);
    }
    sqlite3AggInfoPersistWalkerInit(&w, pParse);
    sqlite3WalkSelect(&w, p);

    p->pSrc = 0;
    p->pWhere = 0;
    p->pGroupBy = 0;
    p->pHaving = 0;
    p->selFlags &= ~SF_Aggregate;
    p->selFlags |= SF_WinRewrite;

    /* 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 = exprListAppendList(pParse, 0, pMWin->pPartition, 1);
    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
    );
    SELECTTRACE(1,pParse,pSub,
       ("New window-function subquery in FROM clause of (%u/%p)\n",
       p->selId, p));
    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);
      pSub->selFlags |= (selFlags & SF_Aggregate);
      if( pTab2==0 ){
        /* Might actually be some other kind of error, but in that case
        ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
        ** the correct error message regardless. */
        rc = SQLITE_NOMEM;
      }else{
        memcpy(pTab, pTab2, sizeof(Table));
        pTab->tabFlags |= TF_Ephemeral;
        p->pSrc->a[0].pTab = pTab;
        pTab = pTab2;
        memset(&w, 0, sizeof(w));
        w.xExprCallback = sqlite3WindowExtraAggFuncDepth;
        w.xSelectCallback = sqlite3WalkerDepthIncrease;
        w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
        sqlite3WalkSelect(&w, pSub);
      }
    }else{
      sqlite3SelectDelete(db, pSub);
    }
    if( db->mallocFailed ) rc = SQLITE_NOMEM;
    sqlite3DbFree(db, pTab);
  }

  if( rc ){
    if( pParse->nErr==0 ){
      assert( pParse->db->mallocFailed );
      sqlite3ErrorToParser(pParse->db, SQLITE_NOMEM);
    }
  }
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x70(%rax)
je 0x43f3b2
movq -0x18(%rbp), %rax
cmpq $0x0, 0x50(%rax)
jne 0x43f3b2
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x100000, %eax         # imm = 0x100000
cmpl $0x0, %eax
jne 0x43f3b2
movq -0x10(%rbp), %rdi
callq 0x4299d0
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 -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0xb4(%rbp)
movq -0x30(%rbp), %rdi
movl $0x80, %esi
callq 0x3c9520
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
jne 0x43ee85
movq -0x30(%rbp), %rdi
movl $0x7, %esi
callq 0x3d6170
movl %eax, -0x4(%rbp)
jmp 0x43f3db
movq -0x10(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x445a50
movq -0x18(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x42f7d0
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 0x4(%rax), %ecx
andl $-0x9, %ecx
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
orl $0x100000, %ecx         # imm = 0x100000
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rdi
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rdx
xorl %eax, %eax
movl %eax, %esi
movl $0x1, %ecx
callq 0x445aa0
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rdx
movl $0x1, %ecx
callq 0x445aa0
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x43efb2
movq -0x18(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x43efb2
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movl (%rax), %eax
movq -0x60(%rbp), %rcx
cmpl (%rcx), %eax
jg 0x43efb2
movq -0x60(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xb8(%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 0x430350
cmpl $0x0, %eax
jne 0x43efa6
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rsi
callq 0x40c6a0
movq -0x18(%rbp), %rax
movq $0x0, 0x48(%rax)
movl -0xb8(%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 0x20(%rax), %rcx
movq -0x80(%rbp), %r8
leaq -0x68(%rbp), %r9
callq 0x445c00
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 0x445c00
cmpq $0x0, -0x68(%rbp)
je 0x43f02c
movq -0x68(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xd4(%rbp)
jmp 0x43f036
xorl %eax, %eax
movl %eax, -0xd4(%rbp)
jmp 0x43f036
movl -0xd4(%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 0x445aa0
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rdx
xorl %ecx, %ecx
callq 0x445aa0
movq %rax, -0x68(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
je 0x43f1e0
movq -0x78(%rbp), %rax
movq 0x70(%rax), %rax
movq 0x20(%rax), %rax
movq %rax, -0xc0(%rbp)
movq -0x78(%rbp), %rax
movq 0x50(%rax), %rax
movl 0x4(%rax), %eax
andl $0x100000, %eax         # imm = 0x100000
cmpl $0x0, %eax
je 0x43f10d
movq -0x10(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0xc0(%rbp), %rcx
movq -0x80(%rbp), %r8
leaq -0x68(%rbp), %r9
callq 0x445c00
cmpq $0x0, -0x68(%rbp)
je 0x43f0e9
movq -0x68(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xd8(%rbp)
jmp 0x43f0f3
xorl %eax, %eax
movl %eax, -0xd8(%rbp)
jmp 0x43f0f3
movl -0xd8(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x7c(%rax)
movq -0x78(%rbp), %rax
movb $0x1, 0x8c(%rax)
jmp 0x43f153
cmpq $0x0, -0x68(%rbp)
je 0x43f122
movq -0x68(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xdc(%rbp)
jmp 0x43f12c
xorl %eax, %eax
movl %eax, -0xdc(%rbp)
jmp 0x43f12c
movl -0xdc(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x7c(%rax)
movq -0x10(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0xc0(%rbp), %rdx
xorl %ecx, %ecx
callq 0x445aa0
movq %rax, -0x68(%rbp)
movq -0x78(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x43f190
movq -0x30(%rbp), %rdi
movq -0x78(%rbp), %rax
movq 0x48(%rax), %rsi
xorl %edx, %edx
callq 0x42dee0
movq %rax, -0xc8(%rbp)
movq -0x10(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0xc8(%rbp), %rdx
callq 0x41d3c0
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 $0x48, %esi
xorl %edx, %edx
callq 0x410100
movq -0x78(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x78(%rbp)
jmp 0x43f081
cmpq $0x0, -0x68(%rbp)
jne 0x43f21e
movq -0x10(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x30(%rbp), %rdi
movl $0x98, %esi
leaq 0x1014eb(%rip), %rdx    # 0x5406ed
callq 0x41d620
movq -0xe8(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
movl %eax, %esi
callq 0x41d3c0
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 0x41d1d0
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x41d960
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x43f387
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 0x439ed0
movq -0x38(%rbp), %rax
movl 0x4(%rax), %ecx
orl $0x40, %ecx
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl $0x40, %edx
callq 0x4347c0
movq %rax, -0xd0(%rbp)
movl -0xb4(%rbp), %ecx
andl $0x8, %ecx
movq -0x38(%rbp), %rax
orl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
cmpq $0x0, -0xd0(%rbp)
jne 0x43f2fb
movl $0x7, -0x1c(%rbp)
jmp 0x43f385
movq -0x80(%rbp), %rdi
movq -0xd0(%rbp), %rsi
movl $0x80, %edx
callq 0x19610
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 -0xd0(%rbp), %rax
movq %rax, -0x80(%rbp)
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x19360
leaq 0x696e(%rip), %rax      # 0x445cc0
movq %rax, -0xa8(%rbp)
leaq 0x69a0(%rip), %rax      # 0x445d00
movq %rax, -0xa0(%rbp)
leaq 0x69b2(%rip), %rax      # 0x445d20
movq %rax, -0x98(%rbp)
movq -0x38(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x42f7d0
jmp 0x43f394
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x40c7e0
movq -0x30(%rbp), %rax
cmpb $0x0, 0x61(%rax)
je 0x43f3a5
movl $0x7, -0x1c(%rbp)
movq -0x30(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x3c00a0
cmpl $0x0, -0x1c(%rbp)
je 0x43f3d5
movq -0x10(%rbp), %rax
cmpl $0x0, 0x30(%rax)
jne 0x43f3d3
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl $0x7, %esi
callq 0x3d6170
jmp 0x43f3d5
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw (%rax,%rax)