// File: /workspace/github_c_makefile_star2_O3/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
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
jne 0x23cb
cmpl $0x800, 0x4(%rdi)       # imm = 0x800
jne 0x23cb
movl 0x8(%rdi), %r12d
movl 0xc(%rdi), %r13d
movl 0x10(%rdi), %ebx
testl %r14d, %r14d
movq %rdi, 0x8(%rsp)
je 0x23b7
movq %rsi, %r8
testb %cl, %cl
sete %al
testb $0x7, %r13b
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 %r12d, %eax
shrl $0x3, %eax
movq %rax, 0x18(%rsp)
xorl %ebp, %ebp
movq %r12, 0x20(%rsp)
testl %ebx, %ebx
jne 0x22e7
movq %r8, %r15
movq 0x8(%rsp), %rax
movl (%rax), %edi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x10a0
movq %rax, %rbx
testl %ebx, %ebx
je 0x23b9
cmpl $-0x1, %ebx
je 0x23dc
movq %r15, %r8
shll $0x3, %ebx
xorl %r13d, %r13d
movl %r13d, %esi
shrl $0x3, %esi
addq 0x10(%rsp), %rsi
cmpb $0x0, 0x3(%rsp)
je 0x234a
cmpl $0x8, %ebx
jb 0x234a
cmpl %ebx, %r14d
jb 0x234a
movl %ebx, %r12d
shrl $0x3, %r12d
movq %r8, %rdi
movq %r12, %rdx
movl %ebp, %r15d
movl %r14d, %ebp
movq %r8, %r14
callq 0x10e0
movq %r14, %r8
movl %ebp, %r14d
movl %r15d, %ebp
addq %r12, %r8
movq 0x20(%rsp), %r12
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 0x23aa
movzbl (%rsi), %eax
movl %r13d, %ecx
andl $0x7, %ecx
btl %ecx, %eax
movl 0x4(%rsp), %ecx
jae 0x2363
movb (%r8), %al
orb %cl, %al
jmp 0x236a
movl %ecx, %eax
notb %al
andb (%r8), %al
movb %al, (%r8)
incl %r13d
movl %r13d, %eax
xorl %edx, %edx
divl %r12d
decl %ebx
decl %r14d
movl %ecx, %eax
negb %al
movl %edx, %r13d
jo 0x2393
addb %cl, %cl
movl %ecx, 0x4(%rsp)
movl $0x1, %ecx
jmp 0x23aa
incq %r8
testb $0x7, %r13b
sete 0x3(%rsp)
movl $0x1, %ecx
movb $0x1, %al
movl %eax, 0x4(%rsp)
addl %ecx, %ebp
testl %r14d, %r14d
jne 0x22ad
jmp 0x23b9
xorl %ebp, %ebp
movq 0x8(%rsp), %rax
movl %r12d, 0x8(%rax)
movl %r13d, 0xc(%rax)
movl %ebx, 0x10(%rax)
movl %ebp, %edx
movl %edx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1040
cmpl $0xb, (%rax)
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
jne 0x23cb
movl $0x0, (%rax)
xorl %ebx, %ebx
jmp 0x23b9