// File: /workspace/github_c_makefile_star2_O2/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, %ebp
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %dl
orb %al, %dl
cmpb $0x8, %cl
setae %al
orb %dl, %al
pushq $-0x1
popq %rax
jne 0x2438
movq %rdi, %r15
cmpl $0xa41, 0x4(%rdi)       # imm = 0xA41
jne 0x2438
movq %rsi, %r14
pushq $0x1
popq %rax
shll %cl, %eax
movq %rax, 0x8(%rsp)
movl 0x8(%r15), %edx
movl 0x10(%r15), %eax
movl 0xc(%r15), %r13d
addl %eax, %r13d
movl %edx, %ebx
subl %eax, %ebx
testb %cl, %cl
sete %cl
testb $0x7, %r13b
sete %sil
andb %cl, %sil
movb %sil, 0x3(%rsp)
leaq 0x14(%r15), %rcx
movq %rcx, 0x10(%rsp)
xorl %r12d, %r12d
testl %ebp, %ebp
je 0x2430
movl %r12d, 0x4(%rsp)
movl %r13d, %edi
shrl $0x3, %edi
addq 0x10(%rsp), %rdi
cmpb $0x0, 0x3(%rsp)
je 0x2383
cmpl $0x8, %ebx
jb 0x2383
cmpl %ebx, %ebp
jb 0x2383
movl %ebx, %r12d
shrl $0x3, %r12d
movq %r14, %rsi
movq %r12, %rdx
callq 0x10e0
movl %ebx, %ecx
andl $-0x8, %ecx
movl 0x10(%r15), %eax
addl %ecx, %eax
movl %eax, 0x10(%r15)
subl %ecx, %ebp
andl $0x7, %ebx
movl 0x8(%r15), %edx
jmp 0x23f5
movl %r13d, %ecx
andl $0x7, %ecx
pushq $0x1
popq %r10
movl %r10d, %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, %esi
cmovnel %r8d, %esi
movb %sil, (%rdi)
movzbl 0x3(%rsp), %esi
xorl %edi, %edi
testl %ecx, %ecx
sete %dil
leal (%r11,%r11), %ecx
xorl %r12d, %r12d
cmpb $-0x80, %r11b
sete %r12b
movzbl %cl, %ecx
cmovel %r10d, %ecx
movq %rcx, 0x8(%rsp)
cmovnel %esi, %edi
incl %eax
movl %eax, 0x10(%r15)
decl %ebp
decl %ebx
movl %r10d, %ecx
movb %dil, 0x3(%rsp)
addq %r12, %r14
addl %ecx, %r13d
movl 0x4(%rsp), %r12d
addl %ecx, %r12d
cmpl %edx, %eax
jne 0x232f
movq %r15, %rdi
callq 0x2447
cmpl $-0x1, %eax
je 0x2435
cmpl $0x0, 0x10(%r15)
jne 0x2430
movl 0x8(%r15), %edx
movl 0xc(%r15), %r13d
xorl %eax, %eax
movl %edx, %ebx
jmp 0x232f
movl %r12d, %eax
jmp 0x2438
pushq $-0x1
popq %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq