// File: /workspace/github_cpp_makefile_star2_O2/sbingner[P]kerneldec/lzssdec.cpp
// Function: lzssdecompress::decompress(unsigned char*, unsigned int, unsigned int*, unsigned char*, unsigned int, unsigned int*)
void lzssdecompress::decompress(uint8_t *dst, uint32_t dstlen, uint32_t *pdstused, uint8_t *src, uint32_t srclen, uint32_t *psrcused)
{
    _src= src;  _srcend= src+srclen;
    _dst= dst;  _dstend= dst+dstlen;

    while (_src<_srcend && _dst<_dstend)
    {
        switch(_state)
        {
            case EXPECTINGFLAG:
                if (g_debug) fprintf(stderr, "%08x,%08x: flag: %02x\n", _inputoffset, _outputoffset, *_src);
                _flags= *_src++;
                _inputoffset++;
                _bitnr= 0;
                _state= PROCESSFLAGBIT;
                break;
            case PROCESSFLAGBIT:
                if (_flags&1) {
                    if (g_debug) fprintf(stderr, "%08x,%08x: bit%d: %03x copybyte %02x\n", _inputoffset, _outputoffset, _bitnr, _dictptr, *_src);
                    addtodict(*_dst++ = *_src++);
                    _inputoffset++;
                    _outputoffset++;
                    nextflagbit();
                }
                else {
                    _firstbyte= *_src++;
                    _inputoffset++;
                    _state= EXPECTING2NDBYTE;
                }
                break;
            case EXPECTING2NDBYTE:
                {
                    uint8_t secondbyte= *_src++;
                    _inputoffset++;
                    setcounter(_firstbyte, secondbyte);
                    if (g_debug) fprintf(stderr, "%08x,%08x: bit%d: %03x %02x %02x : copy %d bytes from %03x", _inputoffset-2, _outputoffset, _bitnr, _dictptr, _firstbyte, secondbyte, _copycount, _copyptr);
                    if (g_debug) dumpcopydata();
                    _state= COPYFROMDICT;
                }
                break;
            case COPYFROMDICT:
                copyfromdict();
                break;
        }
    }
    if (g_debug) fprintf(stderr, "decompress state= %d, copy: 0x%x, 0x%x\n", _state, _copyptr, _copycount);
    if (pdstused) *pdstused= _dst-dst;
    if (psrcused) *psrcused= _src-src;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, %rbx
movq %rcx, -0x38(%rbp)
movq %rsi, %r12
movq %rdi, %r14
movq %r8, 0x10(%rdi)
movl %r9d, %ecx
addq %r8, %rcx
movq %rcx, 0x18(%rdi)
movq %rsi, 0x20(%rdi)
movl %edx, %eax
addq %rsi, %rax
movq %rax, 0x28(%rdi)
leaq 0x46ea(%rip), %r15      # 0x8290
movq %r8, %rax
cmpq %rcx, %rax
jae 0x3d8b
movq 0x20(%r14), %rcx
cmpq 0x28(%r14), %rcx
jae 0x3d8b
movl (%r14), %edx
cmpq $0x3, %rdx
ja 0x3cad
movslq (%r15,%rdx,4), %rdx
addq %r15, %rdx
jmpq *%rdx
movq %r14, %rdi
callq 0x3eae
jmp 0x3cad
testb $0x1, 0x4(%r14)
jne 0x3c72
leaq 0x1(%rax), %rcx
movq %rcx, 0x10(%r14)
movb (%rax), %al
movb %al, 0x30(%r14)
incl 0x58(%r14)
movl $0x3, (%r14)
jmp 0x3cad
leaq 0x1(%rax), %rcx
movq %rcx, 0x10(%r14)
movzbl (%rax), %r13d
incl 0x58(%r14)
movzbl 0x30(%r14), %esi
movq %r14, %rdi
movl %r13d, %edx
callq 0x3e2a
cmpl $0x0, 0x95c5(%rip)      # 0xd1f8
jne 0x3cba
andl $0x0, (%r14)
jmp 0x3cad
cmpl $0x0, 0x95b2(%rip)      # 0xd1f8
movl 0x58(%r14), %edx
jne 0x3d1d
leaq 0x1(%rax), %rcx
movq %rcx, 0x10(%r14)
movb (%rax), %al
movb %al, 0x4(%r14)
incl %edx
movl %edx, 0x58(%r14)
andl $0x0, 0x8(%r14)
movl $0x2, (%r14)
jmp 0x3cad
cmpl $0x0, 0x957f(%rip)      # 0xd1f8
jne 0x3d4a
leaq 0x1(%rax), %rdx
movq %rdx, 0x10(%r14)
movzbl (%rax), %esi
leaq 0x1(%rcx), %rax
movq %rax, 0x20(%r14)
movb %sil, (%rcx)
movq %r14, %rdi
callq 0x3dec
incl 0x58(%r14)
incl 0x5c(%r14)
movq %r14, %rdi
callq 0x3e0c
movq 0x10(%r14), %rax
movq 0x18(%r14), %rcx
jmp 0x3ba9
movq 0x9317(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
movl 0x58(%r14), %edx
addl $-0x2, %edx
movl 0x5c(%r14), %ecx
movl 0x8(%r14), %r8d
movl 0x4c(%r14), %r9d
movzbl 0x30(%r14), %eax
movq %rax, -0x30(%rbp)
movl 0x54(%r14), %r11d
movl 0x50(%r14), %r10d
leaq 0x45ee(%rip), %rsi      # 0x82dd
xorl %eax, %eax
pushq %r10
pushq %r11
pushq %r13
pushq -0x30(%rbp)
callq 0x3120
addq $0x20, %rsp
cmpl $0x0, 0x94ee(%rip)      # 0xd1f8
je 0x3c39
movq %r14, %rdi
callq 0x3e48
jmp 0x3c39
movq 0x92b4(%rip), %rcx      # 0xcfd8
movq (%rcx), %rdi
movl 0x5c(%r14), %ecx
movzbl (%rax), %r8d
leaq 0x456a(%rip), %rsi      # 0x82a0
xorl %eax, %eax
callq 0x3120
movq 0x10(%r14), %rax
movl 0x58(%r14), %edx
jmp 0x3c50
movq 0x9287(%rip), %rcx      # 0xcfd8
movq (%rcx), %rdi
movl 0x58(%r14), %edx
movl 0x5c(%r14), %ecx
movl 0x8(%r14), %r8d
movl 0x4c(%r14), %r9d
movzbl (%rax), %r10d
subq $0x8, %rsp
leaq 0x4544(%rip), %rsi      # 0x82b7
xorl %eax, %eax
pushq %r10
callq 0x3120
popq %rcx
popq %rdx
movq 0x10(%r14), %rax
movq 0x20(%r14), %rcx
jmp 0x3c7f
cmpl $0x0, 0x9466(%rip)      # 0xd1f8
jne 0x3dc6
movq -0x38(%rbp), %rcx
testq %rcx, %rcx
je 0x3da6
movl 0x20(%r14), %eax
subl %r12d, %eax
movl %eax, (%rcx)
movq 0x10(%rbp), %rcx
testq %rcx, %rcx
je 0x3db7
movl 0x10(%r14), %eax
subl %ebx, %eax
movl %eax, (%rcx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x920b(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
movl (%r14), %edx
movl 0x50(%r14), %ecx
movl 0x54(%r14), %r8d
leaq 0x4536(%rip), %rsi      # 0x8318
xorl %eax, %eax
callq 0x3120
jmp 0x3d94
nop