// File: /workspace/github_cpp_makefile_star2_O3/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 %rdi, %r14
movq %r8, 0x10(%rdi)
movl %r9d, %eax
addq %r8, %rax
movq %rax, 0x18(%rdi)
movq %rsi, 0x20(%rdi)
movl %edx, %eax
movq %rsi, -0x30(%rbp)
addq %rsi, %rax
movq %rax, 0x28(%rdi)
testl %r9d, %r9d
je 0x404c
leaq 0x5460(%rip), %r13      # 0x9290
movq %rbx, %rax
movq 0x20(%r14), %rcx
cmpq 0x28(%r14), %rcx
jae 0x404c
movl (%r14), %edx
cmpq $0x3, %rdx
ja 0x3f7d
movslq (%r13,%rdx,4), %rdx
addq %r13, %rdx
jmpq *%rdx
movq %r14, %rdi
callq 0x4118
jmp 0x3f7d
testb $0x1, 0x4(%r14)
jne 0x3f1d
leaq 0x1(%rax), %rcx
movq %rcx, 0x10(%r14)
movb (%rax), %al
movb %al, 0x30(%r14)
incl 0x58(%r14)
movl $0x3, (%r14)
jmp 0x3f7d
leaq 0x1(%rax), %rcx
movq %rcx, 0x10(%r14)
movzbl (%rax), %r12d
movl 0x58(%r14), %edx
leal 0x1(%rdx), %eax
movl %eax, 0x58(%r14)
movzbl 0x30(%r14), %r11d
movl %r12d, %r10d
shll $0x4, %r10d
andl $0xf00, %r10d           # imm = 0xF00
orl %r11d, %r10d
movl %r10d, 0x50(%r14)
movl %r12d, %r15d
andl $0xf, %r15d
addl 0x48(%r14), %r15d
movl %r15d, 0x54(%r14)
cmpl $0x0, 0xa323(%rip)      # 0xe1f8
jne 0x3f90
movl $0x0, (%r14)
jmp 0x3f7d
cmpl $0x0, 0xa30a(%rip)      # 0xe1f8
movl 0x58(%r14), %edx
jne 0x3fdc
leaq 0x1(%rax), %rcx
movq %rcx, 0x10(%r14)
movb (%rax), %al
movb %al, 0x4(%r14)
incl %edx
movl %edx, 0x58(%r14)
movl $0x0, 0x8(%r14)
movl $0x2, (%r14)
jmp 0x3f7d
cmpl $0x0, 0xa2d4(%rip)      # 0xe1f8
jne 0x4009
leaq 0x1(%rax), %rdx
movq %rdx, 0x10(%r14)
movb (%rax), %al
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%r14)
movb %al, (%rcx)
movq 0x38(%r14), %rcx
movslq 0x4c(%r14), %rdx
leal 0x1(%rdx), %esi
movl %esi, 0x4c(%r14)
movb %al, (%rcx,%rdx)
movl 0x8(%r14), %eax
movl 0x40(%r14), %ecx
decl %ecx
andl %ecx, 0x4c(%r14)
incl 0x58(%r14)
incl 0x5c(%r14)
incl %eax
movl %eax, 0x8(%r14)
shrb 0x4(%r14)
xorl %ecx, %ecx
cmpl $0x8, %eax
setne %cl
incl %ecx
movl %ecx, (%r14)
movq 0x10(%r14), %rax
cmpq 0x18(%r14), %rax
jb 0x3e33
jmp 0x404c
movq 0xa041(%rip), %rax      # 0xdfd8
movq (%rax), %rdi
decl %edx
movl 0x5c(%r14), %ecx
movl 0x8(%r14), %r8d
movl 0x4c(%r14), %r9d
leaq 0x532e(%rip), %rsi      # 0x92dd
xorl %eax, %eax
pushq %r10
pushq %r15
pushq %r12
pushq %r11
callq 0x3120
addq $0x20, %rsp
cmpl $0x0, 0xa22f(%rip)      # 0xe1f8
je 0x3edb
movq %r14, %rdi
callq 0x40ee
jmp 0x3edb
movq 0x9ff5(%rip), %rcx      # 0xdfd8
movq (%rcx), %rdi
movl 0x5c(%r14), %ecx
movzbl (%rax), %r8d
leaq 0x52ab(%rip), %rsi      # 0x92a0
xorl %eax, %eax
callq 0x3120
movq 0x10(%r14), %rax
movl 0x58(%r14), %edx
jmp 0x3ef8
movq 0x9fc8(%rip), %rcx      # 0xdfd8
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 0x5285(%rip), %rsi      # 0x92b7
xorl %eax, %eax
pushq %r10
callq 0x3120
addq $0x10, %rsp
movq 0x10(%r14), %rax
movq 0x20(%r14), %rcx
jmp 0x3f2a
cmpl $0x0, 0xa1a5(%rip)      # 0xe1f8
jne 0x4087
movq 0x10(%rbp), %rax
movq -0x38(%rbp), %rdx
testq %rdx, %rdx
je 0x406b
movl 0x20(%r14), %ecx
subl -0x30(%rbp), %ecx
movl %ecx, (%rdx)
testq %rax, %rax
je 0x4078
movl 0x10(%r14), %ecx
subl %ebx, %ecx
movl %ecx, (%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x3348
jmp 0x4055
nop