// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movq (%rdi), %r12
movq 0x8(%rdi), %rsi
movq 0x18(%rdi), %rdx
leaq (%rdx,%r12), %rcx
movq 0x10(%rsi), %r8
testq %r8, %r8
setne %al
leaq -0x1(%r14), %rdi
cmpq %rcx, %rdi
setb %dil
andb %al, %dil
xorl %ebp, %ebp
cmpb $0x1, %dil
jne 0x44dd0
movq %rcx, %rax
subq %r14, %rax
leaq (%rax,%r15), %rdi
cmpq %r12, %rdi
jbe 0x44c8f
subq %rax, %r12
jbe 0x44cab
movq 0x20(%rbx), %rdi
addq %rax, %rdi
cmpq %rdi, %rcx
jbe 0x44d5f
movq %rbx, %rdi
movq %rax, %rdx
movq %r15, %rcx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x44b03
movq %r14, %r13
negq %r13
testq %r15, %r15
je 0x44d58
movq 0x18(%rbx), %rcx
movq 0x20(%rbx), %rdx
leaq (%rcx,%r15), %rsi
movq %rdx, %rax
subq %rcx, %rax
cmpq %rdx, %rsi
cmovbeq %r15, %rax
testq %rax, %rax
je 0x44cf2
addq 0x10(%rbx), %rcx
leaq (%rcx,%r13), %rdx
xorl %esi, %esi
movb (%rdx,%rsi), %dil
movb %dil, (%rcx,%rsi)
incq %rsi
cmpq %rax, %rsi
jb 0x44ce2
subq %rax, %r15
addq 0x18(%rbx), %rax
movq %rax, 0x18(%rbx)
cmpq 0x20(%rbx), %rax
jne 0x44d3a
movq %rbx, %rdi
callq 0x44991
testl %eax, %eax
je 0x44d4a
cmpq %r14, %r15
movq %r14, %r12
cmovbq %r15, %r12
movq 0x10(%rbx), %rdi
movq 0x20(%rbx), %rsi
addq %rdi, %rsi
addq %r13, %rsi
movq %r12, %rdx
callq 0x4510
movq %r12, 0x18(%rbx)
subq %r12, %r15
movb $0x1, %al
jmp 0x44d4e
testq %r15, %r15
jne 0x44de1
movb $0x1, %al
xorl %r15d, %r15d
jmp 0x44d4e
xorl %eax, %eax
xorl %ebp, %ebp
testb %al, %al
jne 0x44cb1
jmp 0x44dd0
movl $0x1, %ebp
jmp 0x44dd0
addq 0x10(%rbx), %rdx
leaq (%rdx,%r12), %rcx
movq %rsi, %rdi
movq %rax, %rsi
callq *%r8
testl %eax, %eax
je 0x44db4
subq %r12, %r15
addq 0x18(%rbx), %r12
movq %r12, 0x18(%rbx)
cmpq 0x20(%rbx), %r12
jne 0x44dba
movq 0x18(%rbx), %rax
cmpq 0x20(%rbx), %rax
jne 0x44d9b
movq %rbx, %rdi
callq 0x44991
testl %eax, %eax
je 0x44db4
testq %r15, %r15
je 0x44dbe
movq 0x20(%rbx), %rax
cmpq %r15, %rax
cmovaeq %r15, %rax
subq %rax, %r15
movq %rax, 0x18(%rbx)
jmp 0x44d85
xorl %eax, %eax
xorl %ebp, %ebp
jmp 0x44dc8
movb $0x1, %al
jmp 0x44dc8
movl $0x1, %ebp
xorl %eax, %eax
xorl %r15d, %r15d
testb %al, %al
jne 0x44cab
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c799(%rip), %rdi     # 0x61581
leaq 0x1c51f(%rip), %rsi     # 0x6130e
leaq 0x1c799(%rip), %rcx     # 0x6158f
movl $0x2cc, %edx            # imm = 0x2CC
callq 0x4200