// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x28, %rsp
movl %edi, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq 0x18(%rsp), %rax
movl (%rax), %eax
movl %eax, 0xc(%rsp)
movq 0x18(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, 0x8(%rsp)
cmpl $0x0, 0x8(%rsp)
jne 0x45d5
cmpl $0x1, 0xc(%rsp)
jne 0x459f
movl $0x9, 0x24(%rsp)
jmp 0x47cd
movl $0xc, %eax
cmpl 0xc(%rsp), %eax
jg 0x45d0
cmpl $0x2a, 0xc(%rsp)
jg 0x45d0
movl 0xc(%rsp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x45d0
movl 0xc(%rsp), %eax
shll $0x3, %eax
addl $0xb, %eax
movl %eax, 0x24(%rsp)
jmp 0x47cd
jmp 0x47af
cmpl $-0x3, 0xc(%rsp)
jne 0x461f
movl $0xfffffff0, %eax       # imm = 0xFFFFFFF0
cmpl 0x8(%rsp), %eax
jg 0x45fb
cmpl $-0x1, 0x8(%rsp)
jg 0x45fb
movl $0x12, 0x24(%rsp)
jmp 0x47cd
movl $0xffffffb1, %eax       # imm = 0xFFFFFFB1
cmpl 0x8(%rsp), %eax
jg 0x461a
cmpl $-0x10, 0x8(%rsp)
jge 0x461a
movl $0x15, 0x24(%rsp)
jmp 0x47cd
jmp 0x47af
cmpl $0x1, 0xc(%rsp)
jne 0x464a
movl $0xfffffff8, %eax       # imm = 0xFFFFFFF8
cmpl 0x8(%rsp), %eax
jg 0x4645
cmpl $-0x1, 0x8(%rsp)
jg 0x4645
movl $0x6, 0x24(%rsp)
jmp 0x47cd
jmp 0x47af
cmpl $0x2, 0xc(%rsp)
jne 0x4694
movl $0xffffffe0, %eax       # imm = 0xFFFFFFE0
cmpl 0x8(%rsp), %eax
jg 0x4670
cmpl $-0x1, 0x8(%rsp)
jg 0x4670
movl $0xa, 0x24(%rsp)
jmp 0x47cd
movl $0xfffffd00, %eax       # imm = 0xFFFFFD00
cmpl 0x8(%rsp), %eax
jg 0x468f
cmpl $-0x20, 0x8(%rsp)
jge 0x468f
movl $0xd, 0x24(%rsp)
jmp 0x47cd
jmp 0x47af
movl $0x3, %eax
cmpl 0xc(%rsp), %eax
jg 0x47ad
cmpl $0xeff, 0xc(%rsp)       # imm = 0xEFF
jg 0x47ad
movl $0xffff0000, %eax       # imm = 0xFFFF0000
cmpl 0x8(%rsp), %eax
jg 0x47ad
cmpl $-0x1, 0x8(%rsp)
jg 0x47ad
cmpl $0xf, 0xc(%rsp)
jg 0x4707
movl 0xc(%rsp), %eax
movl $0x3, %ecx
cltd
idivl %ecx
shll %eax
addl $0x3, %eax
movl %eax, 0x10(%rsp)
cmpl $0x3, 0xc(%rsp)
jne 0x46f6
movl $0x3, 0x10(%rsp)
cmpl $0xf, 0xc(%rsp)
jne 0x4705
movl $0xb, 0x10(%rsp)
jmp 0x4722
cmpl $0x7f, 0xc(%rsp)
jg 0x4718
movl $0xe, 0x10(%rsp)
jmp 0x4720
movl $0x16, 0x10(%rsp)
jmp 0x4722
movl $0xffffffe0, %eax       # imm = 0xFFFFFFE0
cmpl 0x8(%rsp), %eax
jg 0x473a
movl 0x10(%rsp), %eax
addl $0x7, %eax
movl %eax, 0x10(%rsp)
jmp 0x47a3
movl $0xfffffe00, %eax       # imm = 0xFFFFFE00
cmpl 0x8(%rsp), %eax
jg 0x4752
movl 0x10(%rsp), %eax
addl $0xa, %eax
movl %eax, 0x10(%rsp)
jmp 0x47a1
movl 0x10(%rsp), %eax
addl $0xc, %eax
movl %eax, 0x10(%rsp)
cmpl $0x8000, 0x20(%rsp)     # imm = 0x8000
jbe 0x4774
movl 0x10(%rsp), %eax
addl $0x6, %eax
movl %eax, 0x10(%rsp)
jmp 0x479f
movl $0x400, 0x4(%rsp)       # imm = 0x400
movl 0x20(%rsp), %eax
cmpl 0x4(%rsp), %eax
jbe 0x479d
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
movl 0x4(%rsp), %eax
shll %eax
movl %eax, 0x4(%rsp)
jmp 0x477c
jmp 0x479f
jmp 0x47a1
jmp 0x47a3
movl 0x10(%rsp), %eax
movl %eax, 0x24(%rsp)
jmp 0x47cd
jmp 0x47af
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edx
leaq 0x4d03(%rip), %rdi      # 0x94c1
movb $0x0, %al
callq 0x1040
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
addq $0x28, %rsp
retq
nopw %cs:(%rax,%rax)