// File: /workspace/github_cpp_makefile_star2_O1/dannye[P]jed/src/encoder.cpp
// Function: encodeBlockComponent(BitWriter&, int*, int&, HuffmanTable const&, HuffmanTable const&)
bool encodeBlockComponent(
    BitWriter& bitWriter,
    int* const component,
    int& previousDC,
    const HuffmanTable& dcTable,
    const HuffmanTable& acTable
) {
    // encode DC value
    int coeff = component[0] - previousDC;
    previousDC = component[0];

    uint coeffLength = bitLength(std::abs(coeff));
    if (coeffLength > 11) {
        std::cout << "Error - DC coefficient length greater than 11\n";
        return false;
    }
    if (coeff < 0) {
        coeff += (1 << coeffLength) - 1;
    }

    uint code = 0;
    uint codeLength = 0;
    if (!getCode(dcTable, coeffLength, code, codeLength)) {
        std::cout << "Error - Invalid DC value\n";
        return false;
    }
    bitWriter.writeBits(code, codeLength);
    bitWriter.writeBits(coeff, coeffLength);

    // encode AC values
    for (uint i = 1; i < 64; ++i) {
        // find zero run length
        byte numZeroes = 0;
        while (i < 64 && component[zigZagMap[i]] == 0) {
            numZeroes += 1;
            i += 1;
        }

        if (i == 64) {
            if (!getCode(acTable, 0x00, code, codeLength)) {
                std::cout << "Error - Invalid AC value\n";
                return false;
            }
            bitWriter.writeBits(code, codeLength);
            return true;
        }

        while (numZeroes >= 16) {
            if (!getCode(acTable, 0xF0, code, codeLength)) {
                std::cout << "Error - Invalid AC value\n";
                return false;
            }
            bitWriter.writeBits(code, codeLength);
            numZeroes -= 16;
        }

        // find coeff length
        coeff = component[zigZagMap[i]];
        coeffLength = bitLength(std::abs(coeff));
        if (coeffLength > 10) {
            std::cout << "Error - AC coefficient length greater than 10\n";
            return false;
        }
        if (coeff < 0) {
            coeff += (1 << coeffLength) - 1;
        }

        // find symbol in table
        byte symbol = numZeroes << 4 | coeffLength;
        if (!getCode(acTable, symbol, code, codeLength)) {
            std::cout << "Error - Invalid AC value\n";
            return false;
        }
        bitWriter.writeBits(code, codeLength);
        bitWriter.writeBits(coeff, coeffLength);
    }

    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, %rbx
movq %rcx, %rax
movq %rsi, 0x20(%rsp)
movl (%rsi), %ecx
xorl %ebp, %ebp
movl %ecx, %esi
subl (%rdx), %esi
movl %ecx, (%rdx)
je 0x3273
movl %esi, %ecx
negl %ecx
cmovsl %esi, %ecx
xorl %ebp, %ebp
movl %ecx, %edx
shrl %edx
incl %ebp
cmpl $0x1, %ecx
movl %edx, %ecx
ja 0x3268
cmpl $0xc, %ebp
jb 0x3290
movq 0x3d59(%rip), %rdi      # 0x6fd8
leaq 0x1f35(%rip), %rsi      # 0x51bb
movl $0x2e, %edx
jmp 0x3747
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %ebp, %ecx
shll %cl, %edx
notl %edx
movl %esi, %r12d
sarl $0x1f, %r12d
andl %edx, %r12d
addl %esi, %r12d
movb $0x1, %cl
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %edx, %edx
movq %rdx, %rsi
movzbl (%rax,%rdx), %r11d
incq %rdx
movzbl 0x1(%rax,%rsi), %r8d
cmpb %r8b, %r11b
jae 0x3305
movb $0x1, %r9b
cmpb %bpl, 0x11(%rax,%r11)
jne 0x32d7
movq %r11, %r10
jmp 0x32f5
leaq -0x1(%r8), %r9
cmpq %r11, %r9
je 0x3305
leaq 0x1(%r11), %r10
cmpb %bpl, 0x12(%rax,%r11)
movq %r10, %r11
jne 0x32db
cmpq %r8, %r10
setb %r9b
movl 0xc4(%rax,%r10,4), %r14d
movl %edx, %r15d
testb %r9b, %r9b
jne 0x3312
cmpq $0xf, %rsi
setb %cl
cmpq $0x10, %rdx
jne 0x32b2
testb $0x1, %cl
je 0x3734
movl %r12d, 0x10(%rsp)
testl %r15d, %r15d
movq %rdi, 0x8(%rsp)
je 0x3356
leal -0x1(%r15), %r12d
movl $0x1, %r13d
movl %r14d, %esi
movl %r12d, %ecx
shrl %cl, %esi
movq 0x8(%rsp), %rdi
callq 0x41de
movq 0x8(%rsp), %rdi
incl %r13d
decl %r12d
cmpl %r15d, %r13d
jbe 0x3334
testl %ebp, %ebp
movq %rdi, %r13
movl 0x10(%rsp), %r12d
je 0x3378
decl %ebp
movl %r12d, %esi
movl %ebp, %ecx
shrl %cl, %esi
movq %r13, %rdi
callq 0x41de
addl $-0x1, %ebp
jb 0x3364
movl $0x1, %eax
movl %ecx, 0x4(%rsp)
movq 0x20(%rsp), %rdx
movl %eax, %ecx
cmpl $0x40, %eax
movl $0x3f, %esi
cmovbl %esi, %eax
leaq 0x1f56(%rip), %rsi      # 0x52f0
movl %eax, %edi
incl %edi
xorl %r10d, %r10d
movzbl (%rcx,%rsi), %eax
cmpl $0x0, (%rdx,%rax,4)
jne 0x33c0
incb %r10b
leaq 0x1(%rcx), %rax
cmpq $0x3f, %rcx
movq %rax, %rcx
jb 0x33a1
movq %rdi, %rcx
jmp 0x33c0
movq %rcx, 0x30(%rsp)
cmpl $0x40, %ecx
jne 0x346f
movb $0x1, %al
xorl %ecx, %ecx
movq %rcx, %rdx
movzbl (%rbx,%rcx), %esi
incq %rcx
movzbl 0x1(%rbx,%rdx), %edi
cmpb %dil, %sil
jae 0x3423
cmpb $0x0, 0x11(%rbx,%rsi)
je 0x3410
leaq -0x1(%rdi), %r8
cmpq %rsi, %r8
je 0x3423
leaq 0x1(%rsi), %r9
cmpb $0x0, 0x12(%rbx,%rsi)
movq %r9, %rsi
jne 0x33f1
cmpq %rdi, %r9
setb %dil
movq %r9, %rsi
jmp 0x3413
movb $0x1, %dil
movl 0xc4(%rbx,%rsi,4), %r14d
movl %ecx, %r15d
testb %dil, %dil
jne 0x3430
cmpq $0xf, %rdx
setb %al
cmpq $0x10, %rcx
jne 0x33d2
testb $0x1, %al
je 0x3563
movb $0x1, %al
testl %r15d, %r15d
je 0x36d6
movl %eax, 0x4(%rsp)
leal -0x1(%r15), %r12d
movl $0x1, %ebp
movl %r14d, %esi
movl %r12d, %ecx
shrl %cl, %esi
movq %r13, %rdi
callq 0x41de
incl %ebp
decl %r12d
cmpl %r15d, %ebp
jbe 0x3450
jmp 0x36da
cmpb $0x10, %r10b
jb 0x352e
movb $0x1, %al
xorl %ecx, %ecx
movq %rcx, %rdx
movzbl (%rbx,%rcx), %esi
incq %rcx
movzbl 0x1(%rbx,%rdx), %edi
cmpb %dil, %sil
jae 0x34ce
movb $0x1, %r8b
cmpb $-0x10, 0x11(%rbx,%rsi)
jne 0x34a0
movq %rsi, %r9
jmp 0x34be
leaq -0x1(%rdi), %r8
cmpq %rsi, %r8
je 0x34ce
leaq 0x1(%rsi), %r9
cmpb $-0x10, 0x12(%rbx,%rsi)
movq %r9, %rsi
jne 0x34a4
cmpq %rdi, %r9
setb %r8b
movl 0xc4(%rbx,%r9,4), %r14d
movl %ecx, %r15d
testb %r8b, %r8b
jne 0x34db
cmpq $0xf, %rdx
setb %al
cmpq $0x10, %rcx
jne 0x347d
testb $0x1, %al
je 0x36ba
movl %r10d, 0x10(%rsp)
testl %r15d, %r15d
movq %r14, %r12
movq 0x8(%rsp), %r14
je 0x3518
leal -0x1(%r15), %r13d
movl $0x1, %ebp
movl %r12d, %esi
movl %r13d, %ecx
shrl %cl, %esi
movq %r14, %rdi
callq 0x41de
incl %ebp
decl %r13d
cmpl %r15d, %ebp
jbe 0x34fe
movl 0x10(%rsp), %r10d
addb $-0x10, %r10b
cmpb $0xf, %r10b
movq %r12, %r14
ja 0x3479
movl 0x30(%rsp), %eax
leaq 0x1db7(%rip), %rcx      # 0x52f0
movzbl (%rax,%rcx), %eax
movq 0x20(%rsp), %rcx
movl (%rcx,%rax,4), %eax
testl %eax, %eax
je 0x358a
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
xorl %r13d, %r13d
movl %ecx, %edx
shrl %edx
incl %r13d
cmpl $0x1, %ecx
movl %edx, %ecx
ja 0x3555
jmp 0x358d
movl $0x19, %edx
movq 0x3a69(%rip), %rdi      # 0x6fd8
leaq 0x1c8e(%rip), %rsi      # 0x5204
callq 0x2120
xorl %ebp, %ebp
movl $0x0, 0x4(%rsp)
jmp 0x3703
xorl %r13d, %r13d
cmpl $0xb, %r13d
jb 0x35ab
movl $0x2e, %edx
movq 0x3a39(%rip), %rdi      # 0x6fd8
leaq 0x1c78(%rip), %rsi      # 0x521e
jmp 0x36cd
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %r13d, %ecx
shll %cl, %edx
notl %edx
movl %eax, %r9d
sarl $0x1f, %r9d
andl %edx, %r9d
addl %eax, %r9d
shlb $0x4, %r10b
orb %r13b, %r10b
movb $0x1, %bpl
xorl %eax, %eax
movq %rax, %rcx
movzbl (%rbx,%rax), %edx
incq %rax
movzbl 0x1(%rbx,%rcx), %esi
cmpb %sil, %dl
jae 0x3621
movb $0x1, %dil
cmpb %r10b, 0x11(%rbx,%rdx)
jne 0x35f3
movq %rdx, %r8
jmp 0x3611
leaq -0x1(%rsi), %rdi
cmpq %rdx, %rdi
je 0x3621
leaq 0x1(%rdx), %r8
cmpb %r10b, 0x12(%rbx,%rdx)
movq %r8, %rdx
jne 0x35f7
cmpq %rsi, %r8
setb %dil
movl 0xc4(%rbx,%r8,4), %r14d
movl %eax, %r15d
testb %dil, %dil
jne 0x362f
cmpq $0xf, %rcx
setb %bpl
cmpq $0x10, %rax
jne 0x35d0
testb $0x1, %bpl
je 0x36de
movl %r9d, 0x2c(%rsp)
movl %ebp, 0x1c(%rsp)
testl %r15d, %r15d
movq %r15, 0x10(%rsp)
movq %r14, %r15
movq 0x8(%rsp), %r14
je 0x367d
movq 0x10(%rsp), %rcx
leal -0x1(%rcx), %r12d
movl $0x1, %ebp
movl %r15d, %esi
movl %r12d, %ecx
shrl %cl, %esi
movq %r14, %rdi
callq 0x41de
incl %ebp
decl %r12d
cmpl 0x10(%rsp), %ebp
jbe 0x3662
testl %r13d, %r13d
je 0x3721
decl %r13d
movq 0x8(%rsp), %r14
movl 0x1c(%rsp), %ebp
movl 0x2c(%rsp), %r12d
movl %r12d, %esi
movl %r13d, %ecx
shrl %cl, %esi
movq %r14, %rdi
callq 0x41de
addl $-0x1, %r13d
jb 0x3697
movq %r14, %r13
movq %r15, %r14
movq 0x10(%rsp), %r15
jmp 0x3703
movl $0x19, %edx
movq 0x3912(%rip), %rdi      # 0x6fd8
leaq 0x1b37(%rip), %rsi      # 0x5204
callq 0x2120
xorl %ebp, %ebp
jmp 0x36f6
movl %eax, 0x4(%rsp)
xorl %ebp, %ebp
jmp 0x3703
movl $0x19, %edx
movq 0x38ee(%rip), %rdi      # 0x6fd8
leaq 0x1b13(%rip), %rsi      # 0x5204
callq 0x2120
movl $0x0, 0x4(%rsp)
movq 0x8(%rsp), %r13
testb $0x1, %bpl
movq 0x20(%rsp), %rdx
je 0x375f
movq 0x30(%rsp), %rcx
leal 0x1(%rcx), %eax
cmpl $0x3e, %ecx
jbe 0x3386
jmp 0x3765
movq 0x8(%rsp), %r13
movq %r15, %r14
movq 0x10(%rsp), %r15
movl 0x1c(%rsp), %ebp
jmp 0x3703
movq 0x389d(%rip), %rdi      # 0x6fd8
leaq 0x1aa8(%rip), %rsi      # 0x51ea
movl $0x19, %edx
callq 0x2120
xorl %eax, %eax
andb $0x1, %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x4(%rsp), %eax
jmp 0x374e
movb $0x1, %al
jmp 0x374e