// File: /workspace/github_c_makefile_star2_O1/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 0x5595
cmpl $0x1, %esi
jne 0x55aa
callq 0x56fd
movl %eax, 0x8d6c5(%rip)     # 0x92c34
cmpl $-0x1, %eax
je 0x55b6
leaq 0x5da5(%rip), %rax      # 0xb320
xorl %ecx, %ecx
cmpl $0x0, 0x10(%rax)
setne %cl
leal 0x8(,%rcx,8), %eax
movl %eax, 0x8d6a7(%rip)     # 0x92c38
xorl %ebx, %ebx
jmp 0x55bb
movl %edi, %r14d
leal -0x20(%r14), %eax
cmpl $-0x1f, %eax
jae 0x55c9
leaq 0x229c(%rip), %rdi      # 0x7844
jmp 0x55b1
leaq 0x225d(%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
movl 0x8d669(%rip), %ecx     # 0x92c38
xorl %ebx, %ebx
movl 0x8d65d(%rip), %eax     # 0x92c34
leaq 0x5d42(%rip), %r12      # 0xb320
movl 0x18(%r12), %edx
orl %ecx, %edx
movl %eax, %r15d
jne 0x5615
callq 0x56fd
movl %eax, 0x8d63f(%rip)     # 0x92c34
cmpl $-0x1, %eax
je 0x55b6
movl %eax, %r15d
xorl %eax, %eax
cmpl $0x0, 0x10(%r12)
setne %al
leal 0x8(,%rax,8), %ecx
movl %ecx, 0x8d623(%rip)     # 0x92c38
leal (%r15,%r15), %eax
movl %eax, 0x8d615(%rip)     # 0x92c34
decl %ecx
movl %ecx, 0x8d611(%rip)     # 0x92c38
cmpl $0x0, 0x18(%r12)
je 0x565f
testl %ecx, %ecx
jne 0x565f
callq 0x56fd
movl %eax, 0x8d5f6(%rip)     # 0x92c34
cmpl $-0x1, %eax
je 0x55b6
xorl %ecx, %ecx
cmpl $0x0, 0x10(%r12)
setne %cl
leal 0x8(,%rcx,8), %ecx
movl %ecx, 0x8d5d9(%rip)     # 0x92c38
shldl $0x1, %r15d, %ebx
decl %r14d
jne 0x55de
jmp 0x55bb