// File: sisong[P]ApkDiffPatch/HDiffPatch/libHDiffPatch/HDiff/private_diff/limit_mem_diff/stream_serialize.cpp
// Function: hdiff_private::TStepStream::doStep()
bool TStepStream::doStep(){
    if (pCurCover==0){
        if (isHaveLeftCover)      { isHaveLeftCover=false;  pCurCover=&leftCover;  }
        else if (isHaveRightCover){ isHaveRightCover=false; pCurCover=&rightCover; }
        else if (cur_i<covers.coverCount()) { covers.covers(cur_i++,&cover); pCurCover=&cover; }
        else if (isHaveLastCover) { isHaveLastCover=false;  pCurCover=&lastCover;  }
        else if (step_bufCover_size>0){ _last_flush_step(); return true; }
        else { return false; } //end while
    }
    cover=*pCurCover;
    const size_t step_bufCover_backSize=step_bufCover_size;

    hpatch_StreamPos_t backNewLen=cover.newPos-lastNewEnd;
    if (cover.oldPos>=lastOldEnd){ //save inc_oldPos
        if (isInInit)
            step_bufCover_size+=hpatch_packUIntWithTag_size((hpatch_StreamPos_t)(cover.oldPos-lastOldEnd), 1);
        else 
            packUIntWithTag(step_bufCover,(hpatch_StreamPos_t)(cover.oldPos-lastOldEnd), 0, 1);
    }else{
        if (isInInit)
            step_bufCover_size+=hpatch_packUIntWithTag_size((hpatch_StreamPos_t)(lastOldEnd-cover.oldPos), 1);
        else
            packUIntWithTag(step_bufCover,(hpatch_StreamPos_t)(lastOldEnd-cover.oldPos), 1, 1);//sub safe
    }
    if (isInInit){
        step_bufCover_size+=hpatch_packUInt_size(backNewLen)+hpatch_packUInt_size(cover.length);
    }else{
        packUInt(step_bufCover,backNewLen); //save inc_newPos
        packUInt(step_bufCover,cover.length);
        step_bufCover_size=step_bufCover.size();
    }
    
    subDiff.resetCover(cover);
    const hpatch_StreamPos_t curMaxNeedSize = step_bufCover_size +
            (subDiff.isZeroSubDiff?step_bufRle.maxCodeSizeByZeroLen(subDiff.streamSize):step_bufRle.maxCodeSize(&subDiff));
    if (curMaxNeedSize<=patchStepMemSize){ //append
        if (subDiff.isZeroSubDiff)
            step_bufRle.appendByZeroLen(subDiff.streamSize);
        else
            step_bufRle.append(&subDiff);
        if (backNewLen>0){
            newDataDiffReadPos+=backNewLen;
            step_dataDiffSize+=backNewLen;
        }

        lastOldEnd=cover.oldPos+cover.length;//! +length
        lastNewEnd=cover.newPos+cover.length;
        pCurCover=0; // next 
        return true;
    }else{
        if (step_bufCover_backSize+step_bufRle.curCodeSize()>=(patchStepMemSize/2)){//flush step
            step_bufCover_size=step_bufCover_backSize;
            if (!isInInit) step_bufCover.resize(step_bufCover_backSize);
            
            step_dataDiff.reset(&newDataDiff,newDataDiffReadPos-step_dataDiffSize,newDataDiffReadPos);
            step_dataDiffSize=0;
            _flush_step_code();
            return true;  // pCurCover!
        }else{ //clip one cover to two cover
            check((!isHaveLeftCover)&&(!isHaveRightCover));
            isHaveLeftCover=true;
            isHaveRightCover=true;
            leftCover=cover;
            rightCover=cover;
            hpatch_StreamPos_t clen=cover.length;
            while (1) {
                clen=clen*3/4;
                subDiff.resetCoverLen(clen);
                check(clen>0); // stepMemSize error
                const hpatch_StreamPos_t _curMaxNeedSize = step_bufCover_size +
                    (subDiff.isZeroSubDiff?step_bufRle.maxCodeSizeByZeroLen(subDiff.streamSize):step_bufRle.maxCodeSize(&subDiff));
                if (_curMaxNeedSize<=patchStepMemSize)
                    break;
            }
            leftCover.length=clen;
            rightCover.length-=clen;
            rightCover.oldPos+=clen;
            rightCover.newPos+=clen;

            step_bufCover_size=step_bufCover_backSize;
            if (!isInInit) step_bufCover.resize(step_bufCover_backSize);
            ++curCoverCount;
            pCurCover=0;
            return true;  // leftCover
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, 0x228(%rax)
jne 0x4e467
movq -0x50(%rbp), %rax
testb $0x1, 0x1b9(%rax)
je 0x4e373
movq -0x50(%rbp), %rax
movb $0x0, 0x1b9(%rax)
movq %rax, %rcx
addq $0x1d8, %rcx            # imm = 0x1D8
movq %rcx, 0x228(%rax)
jmp 0x4e465
movq -0x50(%rbp), %rax
testb $0x1, 0x1ba(%rax)
je 0x4e3a1
movq -0x50(%rbp), %rax
movb $0x0, 0x1ba(%rax)
movq %rax, %rcx
addq $0x1f0, %rcx            # imm = 0x1F0
movq %rcx, 0x228(%rax)
jmp 0x4e463
movq -0x50(%rbp), %rax
movq 0x208(%rax), %rcx
movq %rcx, -0x58(%rbp)
movq 0x110(%rax), %rdi
callq 0x2af30
movq %rax, %rcx
movq -0x58(%rbp), %rax
cmpq %rcx, %rax
jae 0x4e40b
movq -0x50(%rbp), %rdx
movq 0x110(%rdx), %rdi
movq 0x208(%rdx), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, 0x208(%rdx)
addq $0x210, %rdx            # imm = 0x210
callq 0x2af50
movq -0x50(%rbp), %rax
movq %rax, %rcx
addq $0x210, %rcx            # imm = 0x210
movq %rcx, 0x228(%rax)
jmp 0x4e461
movq -0x50(%rbp), %rax
testb $0x1, 0x1b8(%rax)
je 0x4e436
movq -0x50(%rbp), %rax
movb $0x0, 0x1b8(%rax)
movq %rax, %rcx
addq $0x1c0, %rcx            # imm = 0x1C0
movq %rcx, 0x228(%rax)
jmp 0x4e45f
movq -0x50(%rbp), %rax
cmpq $0x0, 0x240(%rax)
jbe 0x4e456
movq -0x50(%rbp), %rdi
callq 0x4edb0
movb $0x1, -0x1(%rbp)
jmp 0x4ea93
movb $0x0, -0x1(%rbp)
jmp 0x4ea93
jmp 0x4e461
jmp 0x4e463
jmp 0x4e465
jmp 0x4e467
movq -0x50(%rbp), %rcx
movq 0x228(%rcx), %rax
movq (%rax), %rdx
movq %rdx, 0x210(%rcx)
movq 0x8(%rax), %rdx
movq %rdx, 0x218(%rcx)
movq 0x10(%rax), %rax
movq %rax, 0x220(%rcx)
movq 0x240(%rcx), %rax
movq %rax, -0x18(%rbp)
movq 0x218(%rcx), %rax
subq 0x150(%rcx), %rax
movq %rax, -0x20(%rbp)
movq 0x210(%rcx), %rax
cmpq 0x148(%rcx), %rax
jb 0x4e52a
movq -0x50(%rbp), %rax
testb $0x1, 0x230(%rax)
je 0x4e500
movq -0x50(%rbp), %rax
movq 0x210(%rax), %rdi
subq 0x148(%rax), %rdi
movl $0x1, %esi
callq 0x859b0
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, %ecx
addq 0x240(%rax), %rcx
movq %rcx, 0x240(%rax)
jmp 0x4e528
movq -0x50(%rbp), %rax
movq %rax, %rdi
addq $0x158, %rdi            # imm = 0x158
movq 0x210(%rax), %rsi
subq 0x148(%rax), %rsi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x4eef0
jmp 0x4e595
movq -0x50(%rbp), %rax
testb $0x1, 0x230(%rax)
je 0x4e56b
movq -0x50(%rbp), %rax
movq 0x148(%rax), %rdi
subq 0x210(%rax), %rdi
movl $0x1, %esi
callq 0x859b0
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, %ecx
addq 0x240(%rax), %rcx
movq %rcx, 0x240(%rax)
jmp 0x4e593
movq -0x50(%rbp), %rax
movq %rax, %rdi
addq $0x158, %rdi            # imm = 0x158
movq 0x148(%rax), %rsi
subq 0x210(%rax), %rsi
movl $0x1, %ecx
movl %ecx, %edx
callq 0x4eef0
jmp 0x4e595
movq -0x50(%rbp), %rax
testb $0x1, 0x230(%rax)
je 0x4e5e1
movq -0x20(%rbp), %rdi
xorl %esi, %esi
callq 0x859b0
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, -0x5c(%rbp)
movq 0x220(%rax), %rdi
xorl %esi, %esi
callq 0x859b0
movl -0x5c(%rbp), %ecx
movl %eax, %edx
movq -0x50(%rbp), %rax
addl %edx, %ecx
movl %ecx, %ecx
addq 0x240(%rax), %rcx
movq %rcx, 0x240(%rax)
jmp 0x4e62d
movq -0x50(%rbp), %rdi
addq $0x158, %rdi            # imm = 0x158
movq -0x20(%rbp), %rsi
callq 0x4efd0
movq -0x50(%rbp), %rax
movq %rax, %rdi
addq $0x158, %rdi            # imm = 0x158
movq 0x220(%rax), %rsi
callq 0x4efd0
movq -0x50(%rbp), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0xbe90
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x240(%rax)
movq -0x50(%rbp), %rsi
movq %rsi, %rdi
addq $0x20, %rdi
addq $0x210, %rsi            # imm = 0x210
callq 0x4db50
movq -0x50(%rbp), %rax
movq 0x240(%rax), %rcx
movq %rcx, -0x68(%rbp)
testb $0x1, 0x40(%rax)
je 0x4e676
movq -0x50(%rbp), %rax
movq %rax, %rdi
addq $0x170, %rdi            # imm = 0x170
movq 0x28(%rax), %rsi
callq 0x38350
movq %rax, -0x70(%rbp)
jmp 0x4e691
movq -0x50(%rbp), %rsi
movq %rsi, %rdi
addq $0x170, %rdi            # imm = 0x170
addq $0x20, %rsi
callq 0x38400
movq %rax, -0x70(%rbp)
movq -0x50(%rbp), %rcx
movq -0x68(%rbp), %rax
movq -0x70(%rbp), %rdx
addq %rdx, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq 0x118(%rcx), %rax
ja 0x4e760
movq -0x50(%rbp), %rax
testb $0x1, 0x40(%rax)
je 0x4e6d8
movq -0x50(%rbp), %rax
movq %rax, %rdi
addq $0x170, %rdi            # imm = 0x170
movq 0x28(%rax), %rsi
callq 0x385b0
jmp 0x4e6ef
movq -0x50(%rbp), %rsi
movq %rsi, %rdi
addq $0x170, %rdi            # imm = 0x170
addq $0x20, %rsi
callq 0x386e0
cmpq $0x0, -0x20(%rbp)
jbe 0x4e71e
movq -0x50(%rbp), %rax
movq -0x20(%rbp), %rcx
addq 0x1b0(%rax), %rcx
movq %rcx, 0x1b0(%rax)
movq -0x20(%rbp), %rcx
addq 0x1a8(%rax), %rcx
movq %rcx, 0x1a8(%rax)
movq -0x50(%rbp), %rax
movq 0x210(%rax), %rcx
addq 0x220(%rax), %rcx
movq %rcx, 0x148(%rax)
movq 0x218(%rax), %rcx
addq 0x220(%rax), %rcx
movq %rcx, 0x150(%rax)
movq $0x0, 0x228(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x4ea93
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rax
movq %rax, -0x78(%rbp)
addq $0x170, %rdi            # imm = 0x170
callq 0x50960
movq -0x50(%rbp), %rcx
movq %rax, %rdx
movq -0x78(%rbp), %rax
addq %rdx, %rax
movq 0x118(%rcx), %rcx
shrq %rcx
cmpq %rcx, %rax
jb 0x4e81c
movq -0x50(%rbp), %rax
movq -0x18(%rbp), %rcx
movq %rcx, 0x240(%rax)
testb $0x1, 0x230(%rax)
jne 0x4e7c5
movq -0x50(%rbp), %rdi
addq $0x158, %rdi            # imm = 0x158
movq -0x18(%rbp), %rsi
callq 0xbeb0
movq -0x50(%rbp), %rax
movq %rax, %rdi
addq $0x260, %rdi            # imm = 0x260
movq %rax, %rsi
addq $0xa8, %rsi
movq 0x1b0(%rax), %rdx
subq 0x1a8(%rax), %rdx
movq 0x1b0(%rax), %rcx
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
callq 0x4f000
movq -0x50(%rbp), %rdi
movq $0x0, 0x1a8(%rdi)
callq 0x4f110
movb $0x1, -0x1(%rbp)
jmp 0x4ea93
movq -0x50(%rbp), %rax
testb $0x1, 0x1b9(%rax)
jne 0x4e836
movq -0x50(%rbp), %rax
testb $0x1, 0x1ba(%rax)
je 0x4e889
movl $0x10, %edi
callq 0x41e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x80(%rbp)
leaq 0x626d3(%rip), %rsi     # 0xb0f24
callq 0x4140
jmp 0x4e858
movq -0x80(%rbp), %rdi
movq 0x9778d(%rip), %rsi     # 0xe5ff0
movq 0x97756(%rip), %rdx     # 0xe5fc0
callq 0x4630
movq -0x80(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x42e0
jmp 0x4eaa1
movq -0x50(%rbp), %rax
movb $0x1, 0x1b9(%rax)
movb $0x1, 0x1ba(%rax)
movq 0x210(%rax), %rcx
movq %rcx, 0x1d8(%rax)
movq 0x218(%rax), %rcx
movq %rcx, 0x1e0(%rax)
movq 0x220(%rax), %rcx
movq %rcx, 0x1e8(%rax)
movq 0x210(%rax), %rcx
movq %rcx, 0x1f0(%rax)
movq 0x218(%rax), %rcx
movq %rcx, 0x1f8(%rax)
movq 0x220(%rax), %rcx
movq %rcx, 0x200(%rax)
movq 0x220(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x50(%rbp), %rdi
imulq $0x3, -0x40(%rbp), %rax
shrq $0x2, %rax
movq %rax, -0x40(%rbp)
addq $0x20, %rdi
movq -0x40(%rbp), %rsi
callq 0x4db90
cmpq $0x0, -0x40(%rbp)
ja 0x4e97b
movl $0x10, %edi
callq 0x41e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x88(%rbp)
leaq 0x6261c(%rip), %rsi     # 0xb0f59
callq 0x4140
jmp 0x4e944
movq -0x88(%rbp), %rdi
movq 0x9769e(%rip), %rsi     # 0xe5ff0
movq 0x97667(%rip), %rdx     # 0xe5fc0
callq 0x4630
movq -0x88(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x42e0
jmp 0x4eaa1
movq -0x50(%rbp), %rax
movq 0x240(%rax), %rcx
movq %rcx, -0x90(%rbp)
testb $0x1, 0x40(%rax)
je 0x4e9b3
movq -0x50(%rbp), %rax
movq %rax, %rdi
addq $0x170, %rdi            # imm = 0x170
movq 0x28(%rax), %rsi
callq 0x38350
movq %rax, -0x98(%rbp)
jmp 0x4e9d1
movq -0x50(%rbp), %rsi
movq %rsi, %rdi
addq $0x170, %rdi            # imm = 0x170
addq $0x20, %rsi
callq 0x38400
movq %rax, -0x98(%rbp)
movq -0x50(%rbp), %rcx
movq -0x90(%rbp), %rax
movq -0x98(%rbp), %rdx
addq %rdx, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq 0x118(%rcx), %rax
ja 0x4e9f9
jmp 0x4e9fe
jmp 0x4e8fa
movq -0x50(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, 0x1e8(%rax)
movq -0x40(%rbp), %rdx
movq 0x200(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x200(%rax)
movq -0x40(%rbp), %rcx
addq 0x1f0(%rax), %rcx
movq %rcx, 0x1f0(%rax)
movq -0x40(%rbp), %rcx
addq 0x1f8(%rax), %rcx
movq %rcx, 0x1f8(%rax)
movq -0x18(%rbp), %rcx
movq %rcx, 0x240(%rax)
testb $0x1, 0x230(%rax)
jne 0x4ea6e
movq -0x50(%rbp), %rdi
addq $0x158, %rdi            # imm = 0x158
movq -0x18(%rbp), %rsi
callq 0xbeb0
movq -0x50(%rbp), %rax
movq 0x128(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x128(%rax)
movq $0x0, 0x228(%rax)
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xa0, %rsp
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x4680
nopw (%rax,%rax)