// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
cmpl $0x2, %esi
je 0x53e3
cmpl $0x1, %esi
jne 0x53f8
callq 0x554f
movl %eax, 0x8d877(%rip)     # 0x92c34
cmpl $-0x1, %eax
je 0x5404
leaq 0x5f57(%rip), %rax      # 0xb320
xorl %ecx, %ecx
cmpl $0x0, 0x10(%rax)
setne %cl
leal 0x8(,%rcx,8), %eax
movl %eax, 0x8d859(%rip)     # 0x92c38
xorl %ebx, %ebx
jmp 0x5409
movl %edi, %r14d
leal -0x20(%r14), %eax
cmpl $-0x1f, %eax
jae 0x5417
leaq 0x244e(%rip), %rdi      # 0x7844
jmp 0x53ff
leaq 0x240f(%rip), %rdi      # 0x780e
callq 0x1120
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0x5f02(%rip), %r12      # 0xb320
movl 0x18(%r12), %ecx
movl 0x8d80f(%rip), %edx     # 0x92c38
xorl %ebx, %ebx
movl 0x8d803(%rip), %eax     # 0x92c34
movl %ecx, %esi
orl %edx, %esi
movl %eax, %r15d
jne 0x5464
callq 0x554f
movl %eax, 0x8d7ef(%rip)     # 0x92c34
cmpl $-0x1, %eax
je 0x5404
movl %eax, %r15d
xorl %eax, %eax
cmpl $0x0, 0x10(%r12)
setne %al
leal 0x8(,%rax,8), %edx
movl 0x18(%r12), %ecx
leal (%r15,%r15), %eax
movl %eax, 0x8d7c6(%rip)     # 0x92c34
decl %edx
movl %edx, 0x8d7c2(%rip)     # 0x92c38
testl %ecx, %ecx
je 0x54af
testl %edx, %edx
jne 0x54af
callq 0x554f
movl %eax, 0x8d7ab(%rip)     # 0x92c34
cmpl $-0x1, %eax
je 0x5404
xorl %edx, %edx
cmpl $0x0, 0x10(%r12)
movl 0x18(%r12), %ecx
setne %dl
leal 0x8(,%rdx,8), %edx
movl %edx, 0x8d789(%rip)     # 0x92c38
shldl $0x1, %r15d, %ebx
decl %r14d
jne 0x5431
jmp 0x5409