// File: /workspace/github_cpp_makefile_star2_O3/dannye[P]jed/src/encoder.cpp
// Function: encodeHuffmanData(BMPImage const&)
std::vector<byte> encodeHuffmanData(const BMPImage& image) {
    std::vector<byte> huffmanData;
    BitWriter bitWriter(huffmanData);

    int previousDCs[3] = { 0 };

    for (uint i = 0; i < 3; ++i) {
        if (!dcTables[i]->set) {
            generateCodes(*dcTables[i]);
            dcTables[i]->set = true;
        }
        if (!acTables[i]->set) {
            generateCodes(*acTables[i]);
            acTables[i]->set = true;
        }
    }

    for (uint y = 0; y < image.blockHeight; ++y) {
        for (uint x = 0; x < image.blockWidth; ++x) {
            for (uint i = 0; i < 3; ++i) {
                if (!encodeBlockComponent(
                        bitWriter,
                        image.blocks[y * image.blockWidth + x][i],
                        previousDCs[i],
                        *dcTables[i],
                        *acTables[i])) {
                    return std::vector<byte>();
                }
            }
        }
    }

    return huffmanData;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r10
movq %rdi, %r15
xorl %eax, %eax
leaq 0x20(%rsp), %rcx
movq %rax, 0x10(%rcx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rcx)
movb $0x0, 0x48(%rsp)
movq %rcx, 0x50(%rsp)
movq %rax, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
leaq 0x3849(%rip), %r9       # 0x6d70
leaq 0x3862(%rip), %r11      # 0x6d90
movq (%r9,%rax,8), %rcx
cmpb $0x0, 0x384(%rcx)
jne 0x357b
movb (%rcx), %r8b
xorl %edx, %edx
xorl %esi, %esi
movb 0x1(%rcx,%rdx), %dil
incq %rdx
cmpb %dil, %r8b
jae 0x3569
movzbl %r8b, %r8d
movl %esi, 0xc4(%rcx,%r8,4)
incl %esi
incq %r8
movzbl (%rcx,%rdx), %edi
cmpq %rdi, %r8
jb 0x3553
addl %esi, %esi
movl %edi, %r8d
cmpq $0x10, %rdx
jne 0x3542
movb $0x1, 0x384(%rcx)
movq (%r11,%rax,8), %rcx
cmpb $0x0, 0x384(%rcx)
jne 0x35c8
movb (%rcx), %r8b
xorl %edx, %edx
xorl %esi, %esi
movb 0x1(%rcx,%rdx), %dil
incq %rdx
cmpb %dil, %r8b
jae 0x35b6
movzbl %r8b, %r8d
movl %esi, 0xc4(%rcx,%r8,4)
incl %esi
incq %r8
movzbl (%rcx,%rdx), %edi
cmpq %rdi, %r8
jb 0x35a0
addl %esi, %esi
movl %edi, %r8d
cmpq $0x10, %rdx
jne 0x358f
movb $0x1, 0x384(%rcx)
incq %rax
cmpq $0x3, %rax
jne 0x352e
movl 0x10(%r10), %eax
movq $0x0, (%rsp)
testl %eax, %eax
je 0x36ea
movl 0x14(%r10), %ecx
leaq 0x10(%rsp), %r12
movq %r10, 0x40(%rsp)
movq %r15, 0x8(%rsp)
testl %ecx, %ecx
je 0x3699
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %ebp, %ebp
movl 0x14(%r10), %eax
imull (%rsp), %eax
addl %r15d, %eax
leaq (%rax,%rax,2), %rsi
shlq $0x8, %rsi
addq 0x8(%r10), %rsi
testl %ebp, %ebp
je 0x363c
cmpl $0x2, %ebp
jne 0x3635
addq $0x200, %rsi            # imm = 0x200
jmp 0x363c
addq $0x100, %rsi            # imm = 0x100
movq %r9, %r14
movq (%r13,%r9), %rcx
movq %r11, %rbx
movq (%r13,%r11), %r8
leaq 0x48(%rsp), %rdi
movq %r12, %rdx
callq 0x3166
testb %al, %al
je 0x36bd
incq %rbp
addq $0x4, %r12
addq $0x8, %r13
cmpq $0x3, %rbp
movq %r14, %r9
movq 0x40(%rsp), %r10
movq %rbx, %r11
jne 0x360c
incl %r15d
movl 0x14(%r10), %ecx
cmpl %ecx, %r15d
leaq 0x10(%rsp), %r12
jb 0x3607
movl 0x10(%r10), %eax
movq 0x8(%rsp), %r15
jmp 0x369b
xorl %ecx, %ecx
movq (%rsp), %rdx
incl %edx
movq %rdx, (%rsp)
cmpl %eax, %edx
jb 0x35fc
movaps 0x20(%rsp), %xmm0
movq 0x30(%rsp), %rax
movq %rax, (%rsp)
jmp 0x36ed
xorps %xmm0, %xmm0
movq 0x8(%rsp), %r15
movups %xmm0, (%r15)
movq $0x0, 0x10(%r15)
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x36f9
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2100
jmp 0x36f9
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movq (%rsp), %rax
movq %rax, 0x10(%r15)
movq %r15, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x3725
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2100
movq %rbx, %rdi
callq 0x21c0