// File: sisong[P]ApkDiffPatch/HDiffPatch/libHDiffPatch/HPatch/patch.c
// Function: TOutStreamCache_copyFromSelf
hpatch_BOOL _TOutStreamCache_copyFromSelf(_TOutStreamCache* self,hpatch_StreamPos_t aheadLength,hpatch_StreamPos_t copyLength){
    //      [          writed                 ]
    //                                        [    cached buf    |        empty buf         ]
    const hpatch_TStreamInput* src=(const hpatch_TStreamInput*)self->dstStream;
    hpatch_StreamPos_t srcPos=self->writeToPos+self->cacheCur-aheadLength;
    if (src->read==0) //can't read
        return _hpatch_FALSE;
    if ((aheadLength<1)|(aheadLength>self->writeToPos+self->cacheCur))
            return _hpatch_FALSE;
    
    if (srcPos+copyLength<=self->writeToPos){//copy from stream
        //    [        copyLength          ]
__copy_in_stream:
        return _TOutStreamCache_copyFromStream(self,src,srcPos,copyLength);
    }else if (srcPos>=self->writeToPos){ //copy in mem
        //                                      [        copyLength        ]
__copy_in_mem:
        while (copyLength>0){
            hpatch_byte* dstBuf=self->cacheBuf+self->cacheCur;
            hpatch_byte* srcBuf=dstBuf-(hpatch_size_t)aheadLength;
            hpatch_size_t runLen=(self->cacheCur+copyLength<=self->cacheEnd)?(hpatch_size_t)copyLength:(self->cacheEnd-self->cacheCur);
            hpatch_size_t i;
            for (i=0;i<runLen;i++)
                dstBuf[i]=srcBuf[i];
            copyLength-=runLen;
            self->cacheCur+=runLen;
            if (self->cacheCur==self->cacheEnd){
                if (!_TOutStreamCache_flush(self))
                    return _hpatch_FALSE;
                runLen=(hpatch_size_t)((aheadLength<=copyLength)?aheadLength:copyLength);
                memmove(self->cacheBuf,self->cacheBuf+self->cacheEnd-(hpatch_size_t)aheadLength,runLen);
                self->cacheCur=runLen;
                copyLength-=runLen;
            }else{
                assert(copyLength==0);
            }
        }
        return hpatch_TRUE;
    }else if (self->writeToPos+self->cacheCur<=srcPos+self->cacheEnd){ 
        // small data in stream，can as copy in mem
        hpatch_byte* dstBuf=self->cacheBuf+self->cacheCur;
        hpatch_size_t runLen=(hpatch_size_t)(self->writeToPos-srcPos);
        if (!src->read(src,srcPos,dstBuf,dstBuf+runLen))
            return _hpatch_FALSE;
        //srcPos+=runLen; //not used
        copyLength-=runLen;
        self->cacheCur+=runLen;
        if (self->cacheCur==self->cacheEnd){
            while (hpatch_TRUE){
                if (self->cacheCur==self->cacheEnd){
                    if (!_TOutStreamCache_flush(self))
                        return _hpatch_FALSE;
                }
                if (copyLength>0){
                    runLen=(self->cacheEnd<=copyLength)?self->cacheEnd:(hpatch_size_t)copyLength;
                    //srcPos+=runLen; //not used
                    copyLength-=runLen;
                    self->cacheCur=runLen;
                }else{
                    return hpatch_TRUE;
                }
            }
        }else{
            goto __copy_in_mem;
        }
    }else{
        goto __copy_in_stream;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
addq 0x18(%rcx), %rax
subq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x877e1
movl $0x0, -0x4(%rbp)
jmp 0x87b3c
cmpq $0x1, -0x18(%rbp)
setb %al
andb $0x1, %al
movzbl %al, %eax
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rdx
movq (%rdx), %rdx
movq -0x10(%rbp), %rsi
addq 0x18(%rsi), %rdx
cmpq %rdx, %rcx
seta %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
cmpl $0x0, %eax
je 0x87820
movl $0x0, -0x4(%rbp)
jmp 0x87b3c
movq -0x30(%rbp), %rax
addq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
ja 0x87850
jmp 0x87833
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0x875d0
movl %eax, -0x4(%rbp)
jmp 0x87b3c
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jb 0x879fc
jmp 0x87863
jmp 0x87865
cmpq $0x0, -0x20(%rbp)
jbe 0x879f0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
addq 0x18(%rcx), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
xorl %ecx, %ecx
subq -0x18(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
addq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x20(%rcx), %rax
ja 0x878b5
movq -0x20(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0x878c9
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x10(%rbp), %rcx
subq 0x18(%rcx), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0x87907
movq -0x40(%rbp), %rax
movq -0x50(%rbp), %rcx
movb (%rax,%rcx), %dl
movq -0x38(%rbp), %rax
movq -0x50(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x878d9
movq -0x48(%rbp), %rcx
movq -0x20(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x48(%rbp), %rcx
movq -0x10(%rbp), %rax
addq 0x18(%rax), %rcx
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jne 0x879c1
movq -0x10(%rbp), %rdi
callq 0x87300
cmpl $0x0, %eax
jne 0x87956
movl $0x0, -0x4(%rbp)
jmp 0x87b3c
movq -0x18(%rbp), %rax
cmpq -0x20(%rbp), %rax
ja 0x8796a
movq -0x18(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x87972
movq -0x20(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x10(%rbp), %rax
addq 0x20(%rax), %rsi
xorl %eax, %eax
subq -0x18(%rbp), %rax
addq %rax, %rsi
movq -0x48(%rbp), %rdx
callq 0x45c0
movq -0x48(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x48(%rbp), %rcx
movq -0x20(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x20(%rbp)
jmp 0x879eb
cmpq $0x0, -0x20(%rbp)
jne 0x879ca
jmp 0x879e9
leaq 0x2d27c(%rip), %rdi     # 0xb4c4d
leaq 0x2d002(%rip), %rsi     # 0xb49da
movl $0x2cc, %edx            # imm = 0x2CC
leaq 0x2d277(%rip), %rcx     # 0xb4c5b
callq 0x4270
jmp 0x879eb
jmp 0x87865
movl $0x1, -0x4(%rbp)
jmp 0x87b3c
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
addq 0x18(%rcx), %rax
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rdx
addq 0x20(%rdx), %rcx
cmpq %rcx, %rax
ja 0x87b37
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
addq 0x18(%rcx), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
subq -0x30(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x58(%rbp), %rcx
addq -0x60(%rbp), %rcx
callq *%rax
cmpl $0x0, %eax
jne 0x87a72
movl $0x0, -0x4(%rbp)
jmp 0x87b3c
movq -0x60(%rbp), %rcx
movq -0x20(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x60(%rbp), %rcx
movq -0x10(%rbp), %rax
addq 0x18(%rax), %rcx
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jne 0x87b32
jmp 0x87aa9
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jne 0x87ad4
movq -0x10(%rbp), %rdi
callq 0x87300
cmpl $0x0, %eax
jne 0x87ad2
movl $0x0, -0x4(%rbp)
jmp 0x87b3c
jmp 0x87ad4
cmpq $0x0, -0x20(%rbp)
jbe 0x87b24
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
cmpq -0x20(%rbp), %rax
ja 0x87af7
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x78(%rbp)
jmp 0x87aff
movq -0x20(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rcx
movq -0x20(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x60(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0x87b2d
movl $0x1, -0x4(%rbp)
jmp 0x87b3c
jmp 0x87aa9
jmp 0x87863
jmp 0x87833
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)