// File: /workspace/github_c_makefile_star2_O2/salextpuru[P]speccy-toochain/src/mhmt/mhmt-depack.c
// Function: depack_getbits
ULONG depack_getbits(ULONG numbits, ULONG operation)
{
    static ULONG bits;
    static ULONG num_bits_left;
    ULONG fetched_bits;

    if( operation==DEPACK_GETBITS_FORCE ) { // force word retrieval (for start of stream)
        bits = depack_getbits_word();

        if( bits==0xFFFFFFFF) return 0xFFFFFFFF;

        num_bits_left = wrk.wordbit ? 16 : 8;
        return 0;

    } else if( operation==DEPACK_GETBITS_NEXT ) { // return bits and fetch new as needed (wrk.fullbits accounted for)
        if( (numbits==0) || (numbits>31) ) {
            printf("mhmt-depack.c:depack_getbits() - too many (>31) or zero bits requested\n");
            return 0xFFFFFFFF;
        }

        fetched_bits = 0;

        do {
            if( !wrk.fullbits ) { // empty bits
                if( !num_bits_left ) {
                    bits = depack_getbits_word();

                    if( bits==0xFFFFFFFF) return 0xFFFFFFFF;

                    num_bits_left = wrk.wordbit ? 16 : 8;
                }
            }

//#ifdef DBG
//			printf("%d",bits>>31);
//#endif
            fetched_bits = ( fetched_bits<<1 ) | ( 1&(bits>>31) );
            bits <<= 1;
            num_bits_left--;

            if( wrk.fullbits ) {
                if( !num_bits_left ) {
                    bits = depack_getbits_word();

                    if( bits==0xFFFFFFFF) return 0xFFFFFFFF;

                    num_bits_left = wrk.wordbit ? 16 : 8;
                }
            }
        } while( --numbits );

        return fetched_bits;

    } else {
        printf("mhmt-depack.c:depack_getbits() - wrong operation code\n");
        return 0xFFFFFFFF;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
cmpl $0x2, %esi
je 0x487a
cmpl $0x1, %esi
jne 0x488f
callq 0x49e9
movl %eax, 0x8d3e0(%rip)     # 0x91c34
cmpl $-0x1, %eax
je 0x489b
leaq 0x5ac0(%rip), %rax      # 0xa320
xorl %ecx, %ecx
cmpl $0x0, 0x10(%rax)
setne %cl
leal 0x8(,%rcx,8), %eax
movl %eax, 0x8d3c2(%rip)     # 0x91c38
xorl %ebx, %ebx
jmp 0x489e
movl %edi, %r14d
leal -0x20(%r14), %eax
cmpl $-0x1f, %eax
jae 0x48a9
leaq 0x1f53(%rip), %rdi      # 0x67e0
jmp 0x4896
leaq 0x1f14(%rip), %rdi      # 0x67aa
callq 0x1110
pushq $-0x1
popq %rbx
movl %ebx, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5a70(%rip), %r12      # 0xa320
movl 0x18(%r12), %eax
xorl %ebx, %ebx
movl 0x8d37b(%rip), %ecx     # 0x91c38
pushq $-0x1
popq %rbp
movl %eax, %edx
orl %ecx, %edx
je 0x48cf
movl 0x8d367(%rip), %r15d    # 0x91c34
jmp 0x48f9
callq 0x49e9
movl %eax, 0x8d35a(%rip)     # 0x91c34
cmpl $-0x1, %eax
je 0x4953
movl %eax, %r15d
xorl %eax, %eax
cmpl $0x0, 0x10(%r12)
setne %al
leal 0x8(,%rax,8), %ecx
movl 0x18(%r12), %eax
leal (%r15,%r15), %edx
movl %edx, 0x8d331(%rip)     # 0x91c34
decl %ecx
movl %ecx, 0x8d32d(%rip)     # 0x91c38
testl %eax, %eax
je 0x4940
testl %ecx, %ecx
jne 0x4940
callq 0x49e9
movl %eax, 0x8d316(%rip)     # 0x91c34
cmpl $-0x1, %eax
je 0x4953
xorl %ecx, %ecx
cmpl $0x0, 0x10(%r12)
movl 0x18(%r12), %eax
setne %cl
leal 0x8(,%rcx,8), %ecx
movl %ecx, 0x8d2f8(%rip)     # 0x91c38
shldl $0x1, %r15d, %ebx
decl %r14d
jne 0x48c0
jmp 0x489e
movl %ebp, %ebx
jmp 0x489e