// File: /workspace/github_c_makefile_star2_O1/salextpuru[P]speccy-toochain/src/mhmt/mhmt-lz.c
// Function: get_lz_price_hrust
ULONG get_lz_price_hrust(ULONG position, struct lzcode * lzcode)
{
    ULONG varbits,varlen;
    LONG length,disp,tmp;
    length = lzcode->length;
    disp   = lzcode->disp;

    if( disp==0 ) {
        if( length==1 ) {
            return 9; // copy-1-byte

        } else if( (12<=length) && (length<=42) && ( !(length&1) ) ) {
            return 11 + 8*length;

        } else
            goto INVALID_CODE_HRUST;

    } else if( length==(-3) ) { // insertion match!
        if( (-16)<=disp && disp<=(-1) ) {
            return 10+8;

        } else if( (-79)<=disp && disp<(-16) ) {
            return 5+8+8;

        } else
            goto INVALID_CODE_HRUST;

    } else if( length==1 ) {
        if( (-8)<=disp && disp<=(-1) )
            return 6;

        else
            goto INVALID_CODE_HRUST;

    } else if( length==2 ) {
        if( (-32)<=disp && disp<=(-1) ) {
            return 10;

        } else if( (-768)<=disp && disp<(-32) ) {
            return 13;

        } else
            goto INVALID_CODE_HRUST;

    } else if (3<=length && length<=3839 && (-65536)<=disp && disp<=(-1) ) {
        // first, calc influence of length
        if( length<=15 ) { // 3..15
            varlen = 3 + ( (length/3)<<1 );

            if( length==3 )  varlen = 3;

            if( length==15 ) varlen = 11;

        } else if( length<=127 ) { // 16..127
            varlen = 14;

        } else { // 128..3839
            varlen = 14+8;
        }

        // add displacement length
        if( (-32)<=disp ) { // ffe0..ffff
            varlen += 7;

        } else if( (-512)<=disp ) { // fe00..ffdf
            varlen += 10;

        } else { // 0000(-65536)..fdff: -513:-1024, -1025:-2048, -2049:-4096, ... ,-32769:-65536
            // bits:                   12           13           14               18
            varlen += 12;

            if( position>32768 ) {
                varlen += 6; // 8bits

            } else {
                tmp = 1024;

                while( position>(ULONG)tmp ) {
                    varlen++;
                    tmp <<= 1;
                }
            }
        }

        return varlen;

    } else {
INVALID_CODE_HRUST:
        printf("mhmt-lz.c:get_lz_price_hrust(): Found invalid code length=%d, displacement=%d\n",length, disp);
        return 0;
    }
}

// Assembly:
pushq %rbx
movq %rsi, %rax
movl (%rsi), %esi
movl 0x4(%rax), %edx
testl %edx, %edx
je 0x2cdf
cmpl $0x2, %esi
je 0x2d14
cmpl $0x1, %esi
je 0x2d08
cmpl $-0x3, %esi
jne 0x2d32
movl $0x12, %ebx
cmpl $-0x11, %edx
ja 0x2d87
leal 0x4f(%rdx), %eax
movl $0x15, %ebx
cmpl $0x3f, %eax
jae 0x2d77
jmp 0x2d87
movl $0x9, %ebx
cmpl $0x1, %esi
je 0x2d87
leal -0xc(%rsi), %eax
cmpl $0x1f, %eax
setae %al
movl %esi, %ecx
orb %al, %cl
testb $0x1, %cl
jne 0x2d77
leal 0xb(,%rsi,8), %ebx
jmp 0x2d87
movl $0x6, %ebx
cmpl $-0x9, %edx
jbe 0x2d77
jmp 0x2d87
movl $0xa, %ebx
cmpl $-0x21, %edx
ja 0x2d87
leal 0x300(%rdx), %eax
movl $0xd, %ebx
cmpl $0x2e0, %eax            # imm = 0x2E0
jae 0x2d77
jmp 0x2d87
leal -0x3(%rsi), %eax
cmpl $0xefd, %eax            # imm = 0xEFD
setae %al
cmpl $0xffff0000, %edx       # imm = 0xFFFF0000
setb %cl
orb %al, %cl
jne 0x2d77
cmpl $0xf, %esi
jg 0x2d8b
movl %esi, %eax
movl $0xaaaaaaab, %ecx       # imm = 0xAAAAAAAB
imulq %rax, %rcx
shrq $0x20, %rcx
andl $-0x2, %ecx
addl $0x3, %ecx
cmpl $0x3, %esi
cmovel %esi, %ecx
cmpl $0xf, %esi
movl $0xb, %ebx
cmovnel %ecx, %ebx
jmp 0x2d9a
leaq 0x36ab(%rip), %rdi      # 0x6429
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x1040
movl %ebx, %eax
popq %rbx
retq
xorl %eax, %eax
cmpl $0x7f, %esi
setg %al
leal 0xe(,%rax,8), %ebx
cmpl $-0x20, %edx
jl 0x2da4
addl $0x7, %ebx
jmp 0x2d87
cmpl $0xfffffe00, %edx       # imm = 0xFFFFFE00
jl 0x2db1
addl $0xa, %ebx
jmp 0x2d87
cmpl $0x8001, %edi           # imm = 0x8001
jb 0x2dbe
addl $0x12, %ebx
jmp 0x2d87
addl $0xc, %ebx
cmpl $0x401, %edi            # imm = 0x401
jb 0x2d87
movl $0x400, %eax            # imm = 0x400
incl %ebx
addl %eax, %eax
cmpl %edi, %eax
jb 0x2dce
jmp 0x2d87