// File: /workspace/github_cpp_makefile_star2_O3/sisong[P]ApkDiffPatch/HDiffPatch/libHDiffPatch/HPatch/patch.c
// Function: TDiffToSingleStream_read
static hpatch_BOOL _TDiffToSingleStream_read(const struct hpatch_TStreamInput* stream,hpatch_StreamPos_t readFromPos,
                                           unsigned char* out_data,unsigned char* out_data_end){
    //[                                                         |readedSize                  ]
    //     [     |cachedBufBegin   _TDiffToSingleStream_kBufSize]
    //                                 readFromPos[out_data       out_data_end]
    TDiffToSingleStream* self=(TDiffToSingleStream*)stream->streamImport;
    hpatch_StreamPos_t readedSize=self->readedSize;
    while (1){
        size_t rLen=out_data_end-out_data;
        if (readFromPos==readedSize){
            hpatch_BOOL result=self->diffStream->read(self->diffStream,readedSize,out_data,out_data_end);
            self->readedSize=readedSize+rLen;
            if ((self->isInSingleStream)||(rLen>_TDiffToSingleStream_kBufSize)){
                self->cachedBufBegin=_TDiffToSingleStream_kBufSize;
            }else{
                //cache
                if (rLen>=_TDiffToSingleStream_kBufSize){
                    memcpy(self->buf,out_data_end-_TDiffToSingleStream_kBufSize,_TDiffToSingleStream_kBufSize);
                    self->cachedBufBegin = 0;
                }else{
                    size_t new_cachedBufBegin;
                    if (self->cachedBufBegin>=rLen){
                        new_cachedBufBegin=self->cachedBufBegin-rLen;
                        memmove(self->buf+new_cachedBufBegin,self->buf+self->cachedBufBegin,_TDiffToSingleStream_kBufSize-self->cachedBufBegin);
                    }else{
                        new_cachedBufBegin=0;
                        memmove(self->buf,self->buf+rLen,_TDiffToSingleStream_kBufSize-rLen);
                    }
                    memcpy(self->buf+(_TDiffToSingleStream_kBufSize-rLen),out_data,rLen);
                    self->cachedBufBegin=new_cachedBufBegin;
                }
            }
            return result;
        }else{
            size_t cachedSize=_TDiffToSingleStream_kBufSize-self->cachedBufBegin;
            size_t bufSize=(size_t)(readedSize-readFromPos);
            if ((readFromPos<readedSize)&(bufSize<=cachedSize)){
                if (rLen>bufSize)
                    rLen=bufSize;
                memcpy(out_data,self->buf+(_TDiffToSingleStream_kBufSize-bufSize),rLen);
                out_data+=rLen;
                readFromPos+=rLen;
                if (out_data==out_data_end) 
                    return hpatch_TRUE;
                else
                    continue;
            }else{
                return _hpatch_FALSE;
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, %rbx
movq (%rdi), %rdx
movq 0x28(%rdx), %r13
leaq 0x3c(%rdx), %r8
movq %rdx, (%rsp)
movq %r12, %r15
subq %r14, %r15
cmpq %rbx, %r13
je 0x46a0a
xorl %ebp, %ebp
movq %r13, %rax
subq %rbx, %rax
jbe 0x46ad1
movl $0x1000, %ecx           # imm = 0x1000
subq 0x30(%rdx), %rcx
cmpq %rcx, %rax
ja 0x46ad1
cmpq %rax, %r15
cmovaeq %rax, %r15
movq %r8, %rsi
subq %rax, %rsi
addq $0x1000, %rsi           # imm = 0x1000
movq %r14, %rdi
movq %r15, %rdx
movq %r8, %rbp
callq 0x4290
movq %rbp, %r8
movq (%rsp), %rdx
addq %r15, %r14
addq %r15, %rbx
cmpq %r12, %r14
jne 0x469a1
movl $0x1, %ebp
jmp 0x46ad1
movq %r8, %rbx
movq 0x20(%rdx), %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq *0x10(%rdi)
movq (%rsp), %rcx
movl %eax, %ebp
addq %r15, %r13
movq %r13, 0x28(%rcx)
movl $0x1000, %r13d          # imm = 0x1000
cmpq $0x1000, %r15           # imm = 0x1000
ja 0x46acd
cmpl $0x0, 0x38(%rcx)
jne 0x46acd
cmpq $0x1000, %r15           # imm = 0x1000
jb 0x46a70
addq $-0x1000, %r12          # imm = 0xF000
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4290
movq (%rsp), %rcx
xorl %r13d, %r13d
jmp 0x46acd
movq 0x30(%rcx), %rax
movq %rax, %r13
subq %r15, %r13
movq %rbx, %r12
jae 0x46a9b
leaq (%r12,%r15), %rsi
movl $0x1000, %ebx           # imm = 0x1000
subq %r15, %rbx
movq %r12, %rdi
movq %rbx, %rdx
callq 0x4510
xorl %r13d, %r13d
jmp 0x46ab8
leaq (%r12,%r13), %rdi
leaq (%r12,%rax), %rsi
movl $0x1000, %ebx           # imm = 0x1000
movl $0x1000, %edx           # imm = 0x1000
subq %rax, %rdx
callq 0x4510
subq %r15, %rbx
addq %rbx, %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4290
movq (%rsp), %rcx
movq %r13, 0x30(%rcx)
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq