// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x18, %rsp
movl %edi, 0x10(%rsp)
movl %esi, 0xc(%rsp)
cmpl $0x1, 0xc(%rsp)
jne 0x87a1
callq 0x8a20
movl %eax, 0x8d556(%rip)     # 0x95cb4
cmpl $-0x1, 0x8d54f(%rip)    # 0x95cb4
jne 0x8774
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
jmp 0x88f7
leaq 0x5c25(%rip), %rax      # 0xe3a0
movl 0x10(%rax), %edx
movl $0x8, %eax
movl $0x10, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, 0x8d524(%rip)     # 0x95cb8
movl $0x0, 0x14(%rsp)
jmp 0x88f7
cmpl $0x2, 0xc(%rsp)
jne 0x88e1
cmpl $0x0, 0x10(%rsp)
je 0x87ba
cmpl $0x1f, 0x10(%rsp)
jbe 0x87d5
leaq 0x201e(%rip), %rdi      # 0xa7df
movb $0x0, %al
callq 0x1040
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
jmp 0x88f7
movl $0x0, 0x8(%rsp)
leaq 0x5bbc(%rip), %rax      # 0xe3a0
cmpl $0x0, 0x18(%rax)
jne 0x8836
cmpl $0x0, 0x8d4c7(%rip)     # 0x95cb8
jne 0x8834
callq 0x8a20
movl %eax, 0x8d4b6(%rip)     # 0x95cb4
cmpl $-0x1, 0x8d4af(%rip)    # 0x95cb4
jne 0x8814
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
jmp 0x88f7
leaq 0x5b85(%rip), %rax      # 0xe3a0
movl 0x10(%rax), %edx
movl $0x8, %eax
movl $0x10, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, 0x8d484(%rip)     # 0x95cb8
jmp 0x8836
movl 0x8(%rsp), %eax
shll %eax
movl 0x8d472(%rip), %ecx     # 0x95cb4
shrl $0x1f, %ecx
andl $0x1, %ecx
orl %ecx, %eax
movl %eax, 0x8(%rsp)
movl 0x8d460(%rip), %eax     # 0x95cb4
shll %eax
movl %eax, 0x8d458(%rip)     # 0x95cb4
movl 0x8d456(%rip), %eax     # 0x95cb8
addl $-0x1, %eax
movl %eax, 0x8d44d(%rip)     # 0x95cb8
leaq 0x5b2e(%rip), %rax      # 0xe3a0
cmpl $0x0, 0x18(%rax)
je 0x88c1
cmpl $0x0, 0x8d439(%rip)     # 0x95cb8
jne 0x88bf
callq 0x8a20
movl %eax, 0x8d428(%rip)     # 0x95cb4
cmpl $-0x1, 0x8d421(%rip)    # 0x95cb4
jne 0x889f
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
jmp 0x88f7
leaq 0x5afa(%rip), %rax      # 0xe3a0
movl 0x10(%rax), %edx
movl $0x8, %eax
movl $0x10, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, 0x8d3f9(%rip)     # 0x95cb8
jmp 0x88c1
jmp 0x88c3
movl 0x10(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x10(%rsp)
cmpl $0x0, %eax
jne 0x87dd
movl 0x8(%rsp), %eax
movl %eax, 0x14(%rsp)
jmp 0x88f7
leaq 0x1f3f(%rip), %rdi      # 0xa827
movb $0x0, %al
callq 0x1040
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
movl 0x14(%rsp), %eax
addq $0x18, %rsp
retq