// File: /workspace/github_c_makefile_star2_O1/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
jne 0x23d7
cmpl $0x800, 0x4(%rdi)       # imm = 0x800
jne 0x23d7
movl 0x8(%rdi), %ebp
movl 0xc(%rdi), %r12d
movl 0x10(%rdi), %ebx
testl %r14d, %r14d
movq %rdi, 0x8(%rsp)
je 0x23de
movq %rsi, %r8
testb %cl, %cl
sete %al
testb $0x7, %r12b
sete %dl
andb %al, %dl
movb %dl, 0x3(%rsp)
movl $0x1, %eax
shll %cl, %eax
movl %eax, 0x4(%rsp)
leaq 0x14(%rdi), %rax
movq %rax, 0x10(%rsp)
movl %ebp, %eax
shrl $0x3, %eax
movq %rax, 0x18(%rsp)
xorl %r13d, %r13d
movq %rbp, 0x20(%rsp)
testl %ebx, %ebx
jne 0x2304
movq %r8, %r15
movq 0x8(%rsp), %rax
movl (%rax), %edi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x10a0
testl %eax, %eax
je 0x23e1
cmpl $-0x1, %eax
je 0x2404
movq %r15, %r8
shll $0x3, %eax
xorl %r12d, %r12d
movl %eax, %ebx
movl %r12d, %esi
shrl $0x3, %esi
addq 0x10(%rsp), %rsi
cmpb $0x0, 0x3(%rsp)
je 0x236a
cmpl $0x8, %ebx
jb 0x236a
cmpl %ebx, %r14d
jb 0x236a
movl %ebx, %ebp
shrl $0x3, %ebp
movq %r8, %rdi
movq %rbp, %rdx
movl %r13d, %r15d
movl %ebx, %r13d
movl %r14d, %ebx
movq %r8, %r14
callq 0x10e0
movq %r14, %r8
movl %ebx, %r14d
movl %r13d, %ebx
movl %r15d, %r13d
addq %rbp, %r8
movq 0x20(%rsp), %rbp
movl %ebx, %ecx
andl $-0x8, %ecx
addl %ecx, %r12d
movl %r12d, %eax
xorl %edx, %edx
divl %ebp
andl $0x7, %ebx
subl %ecx, %r14d
movl %edx, %r12d
jmp 0x23c9
movzbl (%rsi), %eax
movl %r12d, %ecx
andl $0x7, %ecx
btl %ecx, %eax
movl 0x4(%rsp), %ecx
jae 0x2383
movb (%r8), %al
orb %cl, %al
jmp 0x238a
movl %ecx, %eax
notb %al
andb (%r8), %al
movb %al, (%r8)
incl %r12d
movl %r12d, %eax
xorl %edx, %edx
divl %ebp
decl %ebx
decl %r14d
movl %ecx, %eax
negb %al
movl %edx, %r12d
jo 0x23b2
addb %cl, %cl
movl %ecx, 0x4(%rsp)
movl $0x1, %ecx
jmp 0x23c9
incq %r8
testb $0x7, %r12b
sete 0x3(%rsp)
movl $0x1, %ecx
movb $0x1, %al
movl %eax, 0x4(%rsp)
addl %ecx, %r13d
testl %r14d, %r14d
jne 0x22cb
jmp 0x23e1
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
jmp 0x23f3
xorl %r13d, %r13d
movq 0x8(%rsp), %rax
movl %ebp, 0x8(%rax)
movl %r12d, 0xc(%rax)
movl %ebx, 0x10(%rax)
movl %r13d, %ecx
movl %ecx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1040
cmpl $0xb, (%rax)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
jne 0x23f3
movl $0x0, (%rax)
jmp 0x23e1