// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x58, %rsp
movb %cl, %al
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movl %edx, 0x3c(%rsp)
movb %al, 0x3b(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
cmpq $0x0, 0x48(%rsp)
je 0x2f9c
cmpq $0x0, 0x40(%rsp)
je 0x2f9c
movzbl 0x3b(%rsp), %eax
cmpl $0x0, %eax
jl 0x2f9c
movzbl 0x3b(%rsp), %eax
cmpl $0x7, %eax
jle 0x2fa9
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0x32a1
movq 0x48(%rsp), %rax
cmpl $0x800, 0x4(%rax)       # imm = 0x800
je 0x2fc4
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0x32a1
movq 0x48(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x10(%rsp)
movq 0x48(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0xc(%rsp)
movq 0x48(%rsp), %rax
movl 0x10(%rax), %eax
movl %eax, 0x8(%rsp)
movzbl 0x3b(%rsp), %ecx
movl $0x1, %eax
shll %cl, %eax
movb %al, 0x2f(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movzbl 0x2f(%rsp), %ecx
xorl %eax, %eax
cmpl $0x1, %ecx
movb %al, 0x2(%rsp)
jne 0x3023
movl 0xc(%rsp), %eax
andl $0x7, %eax
cmpl $0x0, %eax
sete %al
movb %al, 0x2(%rsp)
movb 0x2(%rsp), %dl
xorl %eax, %eax
movl $0x1, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movb %al, 0x3(%rsp)
cmpl $0x0, 0x3c(%rsp)
jbe 0x3275
cmpl $0x0, 0x8(%rsp)
jne 0x30bd
movq 0x48(%rsp), %rax
movl (%rax), %edi
movq 0x48(%rsp), %rsi
addq $0x14, %rsi
movl 0x10(%rsp), %eax
shrl $0x3, %eax
movl %eax, %eax
movl %eax, %edx
callq 0x10a0
movl %eax, 0x4(%rsp)
cmpl $-0x1, 0x4(%rsp)
jne 0x309c
callq 0x1040
cmpl $0xb, (%rax)
jne 0x308f
callq 0x1040
movl $0x0, (%rax)
jmp 0x3275
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0x32a1
cmpl $0x0, 0x4(%rsp)
jne 0x30a8
jmp 0x3275
jmp 0x30aa
movl $0x0, 0xc(%rsp)
movl 0x4(%rsp), %eax
shll $0x3, %eax
movl %eax, 0x8(%rsp)
movq 0x48(%rsp), %rax
addq $0x14, %rax
movl 0xc(%rsp), %ecx
shrl $0x3, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
movzbl 0x3(%rsp), %eax
cmpl $0x0, %eax
je 0x3177
cmpl $0x7, 0x8(%rsp)
jbe 0x3177
movl 0x3c(%rsp), %eax
cmpl 0x8(%rsp), %eax
jb 0x3177
movl 0x8(%rsp), %eax
shrl $0x3, %eax
movl %eax, 0x1c(%rsp)
movq 0x30(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl 0x1c(%rsp), %eax
movl %eax, %edx
callq 0x10e0
movl 0x1c(%rsp), %ecx
movq 0x30(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
movl 0x1c(%rsp), %eax
shll $0x3, %eax
movl %eax, 0x18(%rsp)
movl 0xc(%rsp), %eax
addl 0x18(%rsp), %eax
xorl %edx, %edx
divl 0x10(%rsp)
movl %edx, 0xc(%rsp)
movl 0x18(%rsp), %ecx
movl 0x8(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x8(%rsp)
movl 0x18(%rsp), %ecx
movl 0x3c(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x3c(%rsp)
movl 0x18(%rsp), %eax
addl 0x14(%rsp), %eax
movl %eax, 0x14(%rsp)
jmp 0x3270
movl 0xc(%rsp), %ecx
andl $0x7, %ecx
movl $0x1, %eax
shll %cl, %eax
movb %al, 0x2e(%rsp)
movq 0x20(%rsp), %rax
movzbl (%rax), %edx
movzbl 0x2e(%rsp), %eax
andl %eax, %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, 0x2d(%rsp)
movzbl 0x2d(%rsp), %eax
cmpl $0x0, %eax
jne 0x31c9
movzbl 0x2f(%rsp), %edx
xorl $-0x1, %edx
movq 0x30(%rsp), %rax
movzbl (%rax), %ecx
andl %edx, %ecx
movb %cl, (%rax)
jmp 0x31da
movzbl 0x2f(%rsp), %edx
movq 0x30(%rsp), %rax
movzbl (%rax), %ecx
orl %edx, %ecx
movb %cl, (%rax)
movl 0xc(%rsp), %eax
addl $0x1, %eax
xorl %edx, %edx
divl 0x10(%rsp)
movl %edx, 0xc(%rsp)
movl 0x8(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x8(%rsp)
movl 0x3c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x3c(%rsp)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
movzbl 0x2f(%rsp), %eax
cmpl $0x80, %eax
jne 0x3263
movb $0x1, 0x2f(%rsp)
movq 0x30(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x30(%rsp)
movzbl 0x2f(%rsp), %ecx
xorl %eax, %eax
cmpl $0x1, %ecx
movb %al, 0x1(%rsp)
jne 0x324c
movl 0xc(%rsp), %eax
andl $0x7, %eax
cmpl $0x0, %eax
sete %al
movb %al, 0x1(%rsp)
movb 0x1(%rsp), %dl
xorl %eax, %eax
movl $0x1, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movb %al, 0x3(%rsp)
jmp 0x326e
movzbl 0x2f(%rsp), %eax
shll %eax
movb %al, 0x2f(%rsp)
jmp 0x3270
jmp 0x3038
movl 0x10(%rsp), %ecx
movq 0x48(%rsp), %rax
movl %ecx, 0x8(%rax)
movl 0xc(%rsp), %ecx
movq 0x48(%rsp), %rax
movl %ecx, 0xc(%rax)
movl 0x8(%rsp), %ecx
movq 0x48(%rsp), %rax
movl %ecx, 0x10(%rax)
movl 0x14(%rsp), %eax
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nopw (%rax,%rax)