// File: /workspace/github_c_makefile_star2_O1/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 0x25b6
movq %rdi, %r15
cmpl $0xa41, 0x4(%rdi)       # imm = 0xA41
jne 0x25b6
testl %ebx, %ebx
je 0x25ca
movq %rsi, %r14
testb %cl, %cl
sete %al
movl 0x10(%r15), %edx
movl 0xc(%r15), %r11d
addl %edx, %r11d
testb $0x7, %r11b
sete %r12b
movl $0x1, %esi
shll %cl, %esi
movq %rsi, 0x8(%rsp)
movl 0x8(%r15), %ebp
andb %al, %r12b
subl %edx, %ebp
leaq 0x14(%r15), %r13
xorl %eax, %eax
movl %r11d, %edi
shrl $0x3, %edi
addq %r13, %rdi
testb %r12b, %r12b
je 0x2504
cmpl $0x8, %ebp
jb 0x2504
cmpl %ebp, %ebx
jb 0x2504
movb %r12b, 0x3(%rsp)
movl %ebp, %r12d
shrl $0x3, %r12d
movq %r14, %rsi
movq %r12, %rdx
movl %ebp, 0x4(%rsp)
movl %eax, %ebp
movq %r14, 0x10(%rsp)
movq %r13, %r14
movq %r15, %r13
movl %ebx, %r15d
movl %r11d, %ebx
callq 0x10e0
movl %ebx, %r11d
movl %r15d, %ebx
movq %r13, %r15
movq %r14, %r13
movq 0x10(%rsp), %r14
movl %ebp, %eax
movl 0x4(%rsp), %ebp
movl %ebp, %r9d
andl $-0x8, %r9d
addl %r9d, 0x10(%r15)
subl %r9d, %ebx
andl $0x7, %ebp
jmp 0x2570
movl %r11d, %ecx
andl $0x7, %ecx
movl $0x1, %r9d
movl $0x1, %edx
shll %cl, %edx
movb (%rdi), %sil
movl %esi, %r8d
orb %dl, %r8b
notb %dl
andb %sil, %dl
movq 0x8(%rsp), %r10
testb %r10b, (%r14)
movzbl %r8b, %esi
movzbl %dl, %edx
cmovnel %esi, %edx
movb %dl, (%rdi)
movzbl %r12b, %edx
xorl %esi, %esi
testl %ecx, %ecx
sete %sil
leal (%r10,%r10), %ecx
xorl %r12d, %r12d
cmpb $-0x80, %r10b
movzbl %cl, %ecx
cmovel %r9d, %ecx
movq %rcx, 0x8(%rsp)
cmovnel %edx, %esi
sete %r12b
incl 0x10(%r15)
decl %ebx
decl %ebp
movb %sil, 0x3(%rsp)
addl %r9d, %eax
movl 0x10(%r15), %ecx
cmpl 0x8(%r15), %ecx
jne 0x25a1
movl %eax, %ebp
movq %r15, %rdi
callq 0x25d2
cmpl $-0x1, %eax
je 0x25b6
cmpl $0x0, 0x10(%r15)
jne 0x25ce
movl 0x8(%r15), %ecx
movl 0xc(%r15), %r11d
movl %ebp, %eax
movl %ecx, %ebp
jmp 0x25a4
addl %r9d, %r11d
addq %r12, %r14
testl %ebx, %ebx
movb 0x3(%rsp), %r12b
jne 0x2495
jmp 0x25bb
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 0x25bb
movl %ebp, %eax
jmp 0x25bb