prevlen = prevpos = 0; prev2 = FALSE;
static void makerle(struct buf *b, termline *ldata,		    void (*makeliteral)(struct buf *b, termchar *c,					unsigned long *state)){    int hdrpos, hdrsize, n, prevlen, prevpos, thislen, thispos, prev2;    termchar *c = ldata->chars;    unsigned long state = 0, oldstate;    n = ldata->cols;    hdrpos = b->len;    hdrsize = 0;    add(b, 0);    prevlen = prevpos = 0;    prev2 = FALSE;    while (n-- > 0) {	thispos = b->len;	makeliteral(b, c++, &state);	thislen = b->len - thispos;	if (thislen == prevlen &&	    !memcmp(b->data + prevpos, b->data + thispos, thislen)) {	    /*	     * This literal precisely matches the previous one.	     * Turn it into a run if it's worthwhile.	     * 	     * With one-byte literals, it costs us two bytes to	     * encode a run, plus another byte to write the header	     * to resume normal output; so a three-element run is	     * neutral, and anything beyond that is unconditionally	     * worthwhile. With two-byte literals or more, even a	     * 2-run is a win.	     */	    if (thislen > 1 || prev2) {		int runpos, runlen;		/*		 * It's worth encoding a run. Start at prevpos,		 * unless hdrsize==0 in which case we can back up		 * another one and start by overwriting hdrpos.		 */		hdrsize--;	       /* remove the literal at prevpos */		if (prev2) {		    assert(hdrsize > 0);		    hdrsize--;		    prevpos -= prevlen;/* and possibly another one */		}		if (hdrsize == 0) {		    assert(prevpos == hdrpos + 1);		    runpos = hdrpos;		    b->len = prevpos+prevlen;		} else {		    memmove(b->data + prevpos+1, b->data + prevpos, prevlen);		    runpos = prevpos;		    b->len = prevpos+prevlen+1;		    /*		     * Terminate the previous run of ordinary		     * literals.		     */		    assert(hdrsize >= 1 && hdrsize <= 128);		    b->data[hdrpos] = hdrsize - 1;		}		runlen = prev2 ? 3 : 2;		while (n > 0 && runlen < 129) {		    int tmppos, tmplen;		    tmppos = b->len;		    oldstate = state;		    makeliteral(b, c, &state);		    tmplen = b->len - tmppos;		    b->len = tmppos;		    if (tmplen != thislen ||			memcmp(b->data + runpos+1, b->data + tmppos, tmplen)) {			state = oldstate;			break;	       /* run over */		    }		    n--, c++, runlen++;		}		assert(runlen >= 2 && runlen <= 129);		b->data[runpos] = runlen + 0x80 - 2;		hdrpos = b->len;		hdrsize = 0;		add(b, 0);		continue;	    } else {		/*		 * Just flag that the previous two literals were		 * identical, in case we find a third identical one		 * we want to turn into a run.		 */		prev2 = TRUE;		prevlen = thislen;		prevpos = thispos;	    }	} else {	    prev2 = FALSE;	    prevlen = thislen;	    prevpos = thispos;	}	/*	 * This character isn't (yet) part of a run. Add it to	 * hdrsize.	 */	hdrsize++;	if (hdrsize == 128) {	    b->data[hdrpos] = hdrsize - 1;	    hdrpos = b->len;	    hdrsize = 0;	    add(b, 0);	    prevlen = prevpos = 0;	    prev2 = FALSE;	}    }    /*     * Clean up.     */    if (hdrsize > 0) {	assert(hdrsize <= 128);	b->data[hdrpos] = hdrsize - 1;    } else {	b->len = hdrpos;    }}