// File: /workspace/github_cpp_makefile_star2_O1/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, %ecx
movq %rsi, -0x30(%rbp)
addq %rsi, %rcx
movq %rcx, 0x28(%rdi)
cmpq %rax, %r8
jae 0x40a2
leaq 0x5400(%rip), %r13      # 0x9290
movq %rbx, %rax
movq 0x20(%r14), %rcx
cmpq 0x28(%r14), %rcx
jae 0x40a2
movl (%r14), %ecx
cmpq $0x3, %rcx
ja 0x3ff4
movslq (%r13,%rcx,4), %rcx
addq %r13, %rcx
jmpq *%rcx
movq %r14, %rdi
callq 0x416e
jmp 0x3ff4
testb $0x1, 0x4(%r14)
jne 0x3f8c
leaq 0x1(%rax), %rcx
movq %rcx, 0x10(%r14)
movb (%rax), %al
movb %al, 0x30(%r14)
incl 0x58(%r14)
movl $0x3, (%r14)
jmp 0x3ff4
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, 0xa2c3(%rip)      # 0xe1f8
jne 0x4007
cmpl $0x0, 0xa2b6(%rip)      # 0xe1f8
je 0x3f4c
movq %r14, %rdi
callq 0x4144
movl $0x0, (%r14)
jmp 0x3ff4
cmpl $0x0, 0xa299(%rip)      # 0xe1f8
jne 0x403e
movq 0x10(%r14), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x10(%r14)
movb (%rax), %al
movb %al, 0x4(%r14)
incl 0x58(%r14)
movl $0x0, 0x8(%r14)
movl $0x2, (%r14)
jmp 0x3ff4
cmpl $0x0, 0xa265(%rip)      # 0xe1f8
jne 0x4067
movq 0x10(%r14), %rax
movq 0x20(%r14), %rcx
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 0x3e93
jmp 0x40a2
movq 0x9fca(%rip), %rax      # 0xdfd8
movq (%rax), %rdi
decl %edx
movl 0x5c(%r14), %ecx
movl 0x8(%r14), %r8d
movl 0x4c(%r14), %r9d
leaq 0x52b7(%rip), %rsi      # 0x92dd
xorl %eax, %eax
pushq %r10
pushq %r15
pushq %r12
pushq %r11
callq 0x3120
addq $0x20, %rsp
jmp 0x3f3b
movq 0x9f93(%rip), %rcx      # 0xdfd8
movq (%rcx), %rdi
movl 0x58(%r14), %edx
movl 0x5c(%r14), %ecx
movzbl (%rax), %r8d
leaq 0x5245(%rip), %rsi      # 0x92a0
xorl %eax, %eax
callq 0x3120
jmp 0x3f65
movq 0x9f6a(%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 0x5227(%rip), %rsi      # 0x92b7
xorl %eax, %eax
pushq %r10
callq 0x3120
addq $0x10, %rsp
jmp 0x3f99
cmpl $0x0, 0xa14f(%rip)      # 0xe1f8
jne 0x40dd
movq 0x10(%rbp), %rax
movq -0x38(%rbp), %rdx
testq %rdx, %rdx
je 0x40c1
movl 0x20(%r14), %ecx
subl -0x30(%rbp), %ecx
movl %ecx, (%rdx)
testq %rax, %rax
je 0x40ce
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 0x40ab
nop