// File: /workspace/github_c_makefile_star2_O2/ijleesw[P]data-compression/src/lz78/bitio.c
// Function: bit_read
int bit_read(bit_file* bfp, char* buff_out, UINTMAX_T n_bits, uint8_t ofs) {
    uint8_t* base;
    uint8_t mask;
    uint8_t r_mask;
    uint8_t writebit;
    const uint8_t* readptr;
    UINTMAX_T buff_ready_bytes;
    UINTMAX_T bits_read = 0;
    UINTMAX_T bits_read_total = 0;
    UINTMAX_T buff_size;
    UINTMAX_T w_start;
    UINTMAX_T w_len;
    UINTMAX_T c;
    uint8_t aligned;

    //if (bfp == NULL || buff_out == NULL || n_bits < 0 || ofs < 0 || ofs > 7)
    if (bfp == NULL || buff_out == NULL || ofs < 0 || ofs > 7)
        return -1;

    if (bfp->mode != ACCESS_READ)
        return -1;

    buff_size = bfp->buff_size;
    w_start = bfp->w_start;
    w_len = bfp->w_len;

    mask = 1 << ofs;
    base = (uint8_t*) buff_out;

    /* Check if input ad output are aligned to byte */
    aligned = (mask == 1 && (w_start % 8 == 0)) ? 1 : 0;

    while (n_bits > 0) {
        /* Buffer refill if needed */
        if (w_len == 0) {
            c = read(bfp->fd, bfp->buff, buff_size / 8);
            if (c == -1) {
                if (errno == EAGAIN) {
                    errno = 0;
                    break;
                } else {
                    return -1;
                }
            } else if (c == 0) {
                break;
            }

            w_start = 0;
            w_len = c * 8;
        }

        readptr = (uint8_t*)&(bfp->buff) + w_start / 8;

        if (aligned && w_len > 7 && n_bits >= w_len) {
            /* Optimization: due to alignment we can use memcpy */
            buff_ready_bytes = w_len / 8;
            memcpy(base, readptr, buff_ready_bytes);
            base += buff_ready_bytes;

            bits_read = buff_ready_bytes * 8;
            w_start = (w_start + bits_read) % buff_size;
            w_len -= bits_read;
            n_bits -= bits_read;
            bits_read_total += bits_read;
        } else {
            /* Single bit read */
            r_mask = 1 << w_start % 8;

            writebit = (*readptr & r_mask) ? 1 : 0;
            if (writebit == 0) {
                *base &= ~mask;
            } else {
                *base |= mask;
            }

            w_start = ((w_start + 1) % buff_size);
            --w_len;
            --n_bits;
            ++bits_read_total;

            if (mask == 0x80) {
                mask = 1;
                ++base;
                aligned = (mask == 1 && (w_start % 8 == 0)) ? 1 : 0;
            } else {
                mask <<= 1;
            }
        }
    }

    /* Update */
    bfp->buff_size = buff_size;
    bfp->w_start = w_start;
    bfp->w_len = w_len;
    
    return bits_read_total;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %r14d
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %dl
orb %al, %dl
cmpb $0x8, %cl
setae %al
orb %dl, %al
pushq $-0x1
popq %rdx
jne 0x229e
cmpl $0x800, 0x4(%rdi)       # imm = 0x800
jne 0x229e
movq %rsi, %r15
movl 0x8(%rdi), %r12d
movl 0xc(%rdi), %r13d
movl 0x10(%rdi), %ebx
pushq $0x1
popq %rax
shll %cl, %eax
movl %eax, 0x8(%rsp)
testb %cl, %cl
sete %al
testb $0x7, %r13b
sete %cl
andb %al, %cl
movb %cl, 0x7(%rsp)
movq %rdi, 0x10(%rsp)
leaq 0x14(%rdi), %rax
movq %rax, 0x18(%rsp)
movl %r12d, %eax
shrl $0x3, %eax
movq %rax, 0x20(%rsp)
xorl %edi, %edi
movl %r12d, 0xc(%rsp)
testl %r14d, %r14d
je 0x228c
testl %ebx, %ebx
jne 0x21b7
movl %edi, %ebp
movq 0x10(%rsp), %rax
movl (%rax), %edi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x10a0
movq %rax, %rbx
testl %ebx, %ebx
je 0x228a
cmpl $-0x1, %ebx
je 0x2278
shll $0x3, %ebx
xorl %r13d, %r13d
movl %ebp, %edi
movl %r13d, %esi
shrl $0x3, %esi
addq 0x18(%rsp), %rsi
cmpb $0x0, 0x7(%rsp)
je 0x2214
cmpl $0x8, %ebx
jb 0x2214
cmpl %ebx, %r14d
jb 0x2214
movl %ebx, %r12d
shrl $0x3, %r12d
movl %r14d, %ebp
movl %edi, %r14d
movq %r15, %rdi
movq %r12, %rdx
callq 0x10e0
movl %r14d, %edi
movl %ebp, %r14d
addq %r12, %r15
movl 0xc(%rsp), %r12d
movl %ebx, %ecx
andl $-0x8, %ecx
addl %ecx, %r13d
movl %r13d, %eax
xorl %edx, %edx
divl %r12d
andl $0x7, %ebx
subl %ecx, %r14d
movl %edx, %r13d
jmp 0x2271
movzbl (%rsi), %eax
movl %r13d, %ecx
andl $0x7, %ecx
btl %ecx, %eax
jae 0x222d
movb (%r15), %al
movl 0x8(%rsp), %ecx
orb %cl, %al
jmp 0x2238
movl 0x8(%rsp), %ecx
movl %ecx, %eax
notb %al
andb (%r15), %al
movb %al, (%r15)
incl %r13d
movl %r13d, %eax
xorl %edx, %edx
divl %r12d
decl %ebx
decl %r14d
movl %ecx, %eax
negb %al
movl %edx, %r13d
jo 0x225c
addb %cl, %cl
movl %ecx, 0x8(%rsp)
jmp 0x226e
incq %r15
testb $0x7, %r13b
sete 0x7(%rsp)
movb $0x1, %al
movl %eax, 0x8(%rsp)
pushq $0x1
popq %rcx
addl %ecx, %edi
jmp 0x2176
callq 0x1040
cmpl $0xb, (%rax)
pushq $-0x1
popq %rdx
jne 0x229e
andl $0x0, (%rax)
xorl %ebx, %ebx
movl %ebp, %edi
movq 0x10(%rsp), %rax
movl %r12d, 0x8(%rax)
movl %r13d, 0xc(%rax)
movl %ebx, 0x10(%rax)
movl %edi, %edx
movl %edx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq