// File: 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 %rbx
subq $0x68, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x30(%rbp), %rcx
movl -0x34(%rbp), %edx
addq %rdx, %rcx
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rcx
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rcx
movl -0x1c(%rbp), %edx
addq %rdx, %rcx
movq %rcx, 0x28(%rax)
movq -0x40(%rbp), %rdx
movq 0x10(%rdx), %rcx
xorl %eax, %eax
cmpq 0x18(%rdx), %rcx
movb %al, -0x41(%rbp)
jae 0x41ad
movq -0x40(%rbp), %rcx
movq 0x20(%rcx), %rax
cmpq 0x28(%rcx), %rax
setb %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0x41b9
jmp 0x43c2
movq -0x40(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x50(%rbp)
subq $0x3, %rax
ja 0x43bd
movq -0x50(%rbp), %rax
leaq 0xc09c(%rip), %rcx      # 0x10274
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x0, 0x11800(%rip)     # 0x159e8
je 0x4214
movq -0x40(%rbp), %rax
movq 0x10deb(%rip), %rcx     # 0x14fe0
movq (%rcx), %rdi
movl 0x58(%rax), %edx
movl 0x5c(%rax), %ecx
movq 0x10(%rax), %rax
movzbl (%rax), %r8d
leaq 0xc077(%rip), %rsi      # 0x10284
movb $0x0, %al
callq 0x3120
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, 0x10(%rax)
movb (%rcx), %cl
movb %cl, 0x4(%rax)
movl 0x58(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x58(%rax)
movl $0x0, 0x8(%rax)
movl $0x2, (%rax)
jmp 0x43bd
movq -0x40(%rbp), %rax
movzbl 0x4(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x42e3
cmpl $0x0, 0x11786(%rip)     # 0x159e8
je 0x4298
movq -0x40(%rbp), %rax
movq 0x10d71(%rip), %rcx     # 0x14fe0
movq (%rcx), %rdi
movl 0x58(%rax), %edx
movl 0x5c(%rax), %ecx
movl 0x8(%rax), %r8d
movl 0x4c(%rax), %r9d
movq 0x10(%rax), %rax
movzbl (%rax), %eax
leaq 0xc00d(%rip), %rsi      # 0x1029b
movl %eax, (%rsp)
movb $0x0, %al
callq 0x3120
movq -0x40(%rbp), %rdi
movq 0x10(%rdi), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rdi)
movb (%rax), %al
movq 0x20(%rdi), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, 0x20(%rdi)
movb %al, (%rcx)
movzbl %al, %esi
callq 0x4440
movq -0x40(%rbp), %rdi
movl 0x58(%rdi), %eax
addl $0x1, %eax
movl %eax, 0x58(%rdi)
movl 0x5c(%rdi), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rdi)
callq 0x4480
jmp 0x430a
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, 0x10(%rax)
movb (%rcx), %cl
movb %cl, 0x30(%rax)
movl 0x58(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x58(%rax)
movl $0x3, (%rax)
jmp 0x43bd
movq -0x40(%rbp), %rdi
movq 0x10(%rdi), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rdi)
movb (%rax), %al
movb %al, -0x35(%rbp)
movl 0x58(%rdi), %eax
addl $0x1, %eax
movl %eax, 0x58(%rdi)
movb 0x30(%rdi), %al
movzbl %al, %esi
movzbl -0x35(%rbp), %edx
callq 0x44c0
cmpl $0x0, 0x116a2(%rip)     # 0x159e8
je 0x4396
movq -0x40(%rbp), %rax
movq 0x10c8d(%rip), %rcx     # 0x14fe0
movq (%rcx), %rdi
movl 0x58(%rax), %edx
subl $0x2, %edx
movl 0x5c(%rax), %ecx
movl 0x8(%rax), %r8d
movl 0x4c(%rax), %r9d
movzbl 0x30(%rax), %ebx
movzbl -0x35(%rbp), %r11d
movl 0x54(%rax), %r10d
movl 0x50(%rax), %eax
leaq 0xbf43(%rip), %rsi      # 0x102c1
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
movb $0x0, %al
callq 0x3120
cmpl $0x0, 0x1164b(%rip)     # 0x159e8
je 0x43a8
movq -0x40(%rbp), %rdi
callq 0x4500
movq -0x40(%rbp), %rax
movl $0x0, (%rax)
jmp 0x43bd
movq -0x40(%rbp), %rdi
callq 0x4590
jmp 0x4188
cmpl $0x0, 0x1161f(%rip)     # 0x159e8
je 0x43f0
movq -0x40(%rbp), %rax
movq 0x10c0a(%rip), %rcx     # 0x14fe0
movq (%rcx), %rdi
movl (%rax), %edx
movl 0x50(%rax), %ecx
movl 0x54(%rax), %r8d
leaq 0xbf13(%rip), %rsi      # 0x102fc
movb $0x0, %al
callq 0x3120
cmpq $0x0, -0x28(%rbp)
je 0x440e
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
subq %rcx, %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, 0x10(%rbp)
je 0x442c
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
subq %rcx, %rax
movl %eax, %ecx
movq 0x10(%rbp), %rax
movl %ecx, (%rax)
addq $0x68, %rsp
popq %rbx
popq %rbp
retq
nopw %cs:(%rax,%rax)