// File: /workspace/github_c_makefile_star2_O3/ijleesw[P]data-compression/src/lz78/bitio.c
// Function: bit_write
int bit_write(bit_file* bfp, const char* buff_in, UINTMAX_T n_bits, uint8_t ofs) {
    UINTMAX_T ret = 0;
    const uint8_t* base;
    uint8_t mask;
    uint8_t readbit;
    uint8_t* writeptr;
    UINTMAX_T pos;
    UINTMAX_T buff_free_bits;
    UINTMAX_T buff_free_bytes;
    UINTMAX_T bits_written = 0;
    uint8_t aligned;

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

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

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

    pos = bfp->w_start + bfp->w_len;
    buff_free_bits = bfp->buff_size - bfp->w_len;

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

    while (n_bits > 0) {
        writeptr = (uint8_t*)&(bfp->buff) + pos / 8;

        if (aligned && buff_free_bits > 7 && n_bits >= buff_free_bits) {
            /* Optimization: due to alignment we can use memcpy */
            buff_free_bytes = buff_free_bits / 8;
            memcpy(writeptr, base, buff_free_bytes);
            base += buff_free_bytes;
            bits_written = buff_free_bytes * 8;

            pos += bits_written;
            bfp->w_len += bits_written;
            n_bits -= bits_written;
            ret += bits_written;
            buff_free_bits -= bits_written;
        } else {
            /* Single bit write */
            readbit = (*base & mask) ? 1 : 0;
            if (readbit == 0) {
                *writeptr &= ~(1 << pos % 8);
            } else {
                *writeptr |= (1 << pos % 8);
            }

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

            ++pos;
            ++(bfp->w_len);
            --(n_bits);
            --buff_free_bits;
            ++ret;
        }

        /* Flush if needed */
        if (bfp->w_len == bfp->buff_size) {
            if (bit_flush(bfp) == -1)
                return -1;
            if (bfp->w_len != 0)
                return ret;
            pos = bfp->w_start + bfp->w_len;
            buff_free_bits = bfp->buff_size;
        }

    }

    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %ebx
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %dl
orb %al, %dl
cmpb $0x8, %cl
setae %al
orb %dl, %al
jne 0x258e
movq %rdi, %r15
cmpl $0xa41, 0x4(%rdi)       # imm = 0xA41
jne 0x258e
testl %ebx, %ebx
je 0x25a2
movq %rsi, %r14
testb %cl, %cl
sete %sil
movl 0x8(%r15), %edx
movl 0x10(%r15), %r10d
movl 0xc(%r15), %r8d
addl %r10d, %r8d
testb $0x7, %r8b
sete %bpl
andb %sil, %bpl
movl $0x1, %eax
shll %cl, %eax
movq %rax, 0x8(%rsp)
movl %edx, %r13d
subl %r10d, %r13d
leaq 0x14(%r15), %rax
movq %rax, 0x10(%rsp)
xorl %eax, %eax
movl %r8d, %edi
shrl $0x3, %edi
addq 0x10(%rsp), %rdi
testb %bpl, %bpl
movl %r8d, 0x4(%rsp)
je 0x24d1
cmpl $0x8, %r13d
jb 0x24d1
cmpl %r13d, %ebx
jb 0x24d1
movb %bpl, 0x3(%rsp)
movl %r13d, %r12d
shrl $0x3, %r12d
movq %r14, %rsi
movq %r12, %rdx
movl %eax, %ebp
callq 0x10e0
movl %ebp, %eax
movl %r13d, %esi
andl $-0x8, %esi
movl 0x10(%r15), %r10d
addl %esi, %r10d
movl %r10d, 0x10(%r15)
subl %esi, %ebx
andl $0x7, %r13d
movl 0x8(%r15), %edx
jmp 0x2545
movl %r8d, %ecx
andl $0x7, %ecx
movl $0x1, %esi
shll %cl, %esi
movb (%rdi), %r8b
movl %r8d, %r9d
orb %sil, %r9b
notb %sil
andb %r8b, %sil
movq 0x8(%rsp), %r11
testb %r11b, (%r14)
movzbl %r9b, %r8d
movzbl %sil, %r9d
cmovnel %r8d, %r9d
movl $0x1, %esi
movb %r9b, (%rdi)
movzbl %bpl, %edi
xorl %r8d, %r8d
testl %ecx, %ecx
sete %r8b
leal (%r11,%r11), %ecx
xorl %r12d, %r12d
cmpb $-0x80, %r11b
movzbl %cl, %ecx
cmovel %esi, %ecx
movq %rcx, 0x8(%rsp)
sete %r12b
cmovnel %edi, %r8d
incl %r10d
movl %r10d, 0x10(%r15)
decl %ebx
decl %r13d
movb %r8b, 0x3(%rsp)
addl %esi, %eax
cmpl %edx, %r10d
jne 0x2574
movl %eax, %ebp
movq %r15, %rdi
callq 0x25aa
cmpl $-0x1, %eax
je 0x258e
cmpl $0x0, 0x10(%r15)
jne 0x25a6
movl 0x8(%r15), %edx
movl 0xc(%r15), %r8d
xorl %r10d, %r10d
movl %edx, %r13d
movl %ebp, %eax
jmp 0x257c
movl 0x4(%rsp), %r8d
addl %esi, %r8d
addq %r12, %r14
testl %ebx, %ebx
movb 0x3(%rsp), %bpl
jne 0x2479
jmp 0x2593
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x2593
movl %ebp, %eax
jmp 0x2593