// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x58, %rsp
movb %cl, %al
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movl %edx, 0x3c(%rsp)
movb %al, 0x3b(%rsp)
movl $0x0, 0x34(%rsp)
movl $0x0, 0x8(%rsp)
cmpq $0x0, 0x48(%rsp)
je 0x32fc
cmpq $0x0, 0x40(%rsp)
je 0x32fc
movzbl 0x3b(%rsp), %eax
cmpl $0x0, %eax
jl 0x32fc
movzbl 0x3b(%rsp), %eax
cmpl $0x7, %eax
jle 0x3309
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0x35da
movq 0x48(%rsp), %rax
cmpl $0xa41, 0x4(%rax)       # imm = 0xA41
je 0x3324
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0x35da
movzbl 0x3b(%rsp), %ecx
movl $0x1, %eax
shll %cl, %eax
movb %al, 0x27(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x48(%rsp), %rax
movl 0xc(%rax), %eax
movq 0x48(%rsp), %rcx
addl 0x10(%rcx), %eax
movl %eax, 0x14(%rsp)
movq 0x48(%rsp), %rax
movl 0x8(%rax), %eax
movq 0x48(%rsp), %rcx
subl 0x10(%rcx), %eax
movl %eax, 0x10(%rsp)
movzbl 0x27(%rsp), %ecx
xorl %eax, %eax
cmpl $0x1, %ecx
movb %al, 0x6(%rsp)
jne 0x3387
movl 0x14(%rsp), %eax
andl $0x7, %eax
cmpl $0x0, %eax
sete %al
movb %al, 0x6(%rsp)
movb 0x6(%rsp), %dl
xorl %eax, %eax
movl $0x1, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movb %al, 0x7(%rsp)
cmpl $0x0, 0x3c(%rsp)
jbe 0x35d2
movq 0x48(%rsp), %rax
addq $0x14, %rax
movl 0x14(%rsp), %ecx
shrl $0x3, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movzbl 0x7(%rsp), %eax
cmpl $0x0, %eax
je 0x346e
cmpl $0x7, 0x10(%rsp)
jbe 0x346e
movl 0x3c(%rsp), %eax
cmpl 0x10(%rsp), %eax
jb 0x346e
movl 0x10(%rsp), %eax
shrl $0x3, %eax
movl %eax, 0xc(%rsp)
movq 0x18(%rsp), %rdi
movq 0x28(%rsp), %rsi
movl 0xc(%rsp), %eax
movl %eax, %edx
callq 0x10e0
movl 0xc(%rsp), %ecx
movq 0x28(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
movl 0xc(%rsp), %eax
shll $0x3, %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
addl 0x14(%rsp), %eax
movl %eax, 0x14(%rsp)
movl 0x8(%rsp), %ecx
movq 0x48(%rsp), %rax
addl 0x10(%rax), %ecx
movl %ecx, 0x10(%rax)
movl 0x8(%rsp), %ecx
movl 0x3c(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x3c(%rsp)
movl 0x8(%rsp), %eax
addl 0x34(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x8(%rsp), %ecx
movl 0x10(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x10(%rsp)
jmp 0x356d
movq 0x28(%rsp), %rax
movzbl (%rax), %edx
movzbl 0x27(%rsp), %eax
andl %eax, %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, 0x26(%rsp)
movzbl 0x26(%rsp), %eax
cmpl $0x0, %eax
jne 0x34b7
movl 0x14(%rsp), %ecx
andl $0x7, %ecx
movl $0x1, %edx
shll %cl, %edx
xorl $-0x1, %edx
movq 0x18(%rsp), %rax
movzbl (%rax), %ecx
andl %edx, %ecx
movb %cl, (%rax)
jmp 0x34d1
movl 0x14(%rsp), %ecx
andl $0x7, %ecx
movl $0x1, %edx
shll %cl, %edx
movq 0x18(%rsp), %rax
movzbl (%rax), %ecx
orl %edx, %ecx
movb %cl, (%rax)
movzbl 0x27(%rsp), %eax
cmpl $0x80, %eax
jne 0x3528
movb $0x1, 0x27(%rsp)
movq 0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x28(%rsp)
movzbl 0x27(%rsp), %ecx
xorl %eax, %eax
cmpl $0x1, %ecx
movb %al, 0x5(%rsp)
jne 0x3511
movl 0x14(%rsp), %eax
andl $0x7, %eax
cmpl $0x0, %eax
sete %al
movb %al, 0x5(%rsp)
movb 0x5(%rsp), %dl
xorl %eax, %eax
movl $0x1, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movb %al, 0x7(%rsp)
jmp 0x3533
movzbl 0x27(%rsp), %eax
shll %eax
movb %al, 0x27(%rsp)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
movq 0x48(%rsp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
movl 0x3c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x3c(%rsp)
movl 0x10(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x10(%rsp)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
movq 0x48(%rsp), %rax
movl 0x10(%rax), %eax
movq 0x48(%rsp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x35cd
movq 0x48(%rsp), %rdi
callq 0x35f0
cmpl $-0x1, %eax
jne 0x3598
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0x35da
movq 0x48(%rsp), %rax
cmpl $0x0, 0x10(%rax)
je 0x35ad
movl 0x34(%rsp), %eax
movl %eax, 0x54(%rsp)
jmp 0x35da
movq 0x48(%rsp), %rax
movl 0xc(%rax), %eax
movq 0x48(%rsp), %rcx
addl 0x10(%rcx), %eax
movl %eax, 0x14(%rsp)
movq 0x48(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x10(%rsp)
jmp 0x339c
movl 0x34(%rsp), %eax
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)