// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
xorl %eax, %eax
leaq 0x10(%rsp), %rcx
movq %rax, 0x10(%rcx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rcx)
movb $0x0, 0x38(%rsp)
movq %rcx, 0x40(%rsp)
movq %rax, 0x28(%rsp)
movl $0x0, 0x30(%rsp)
leaq 0x35c2(%rip), %rcx      # 0x6d70
movq (%rcx,%rax,8), %rcx
cmpb $0x0, 0x384(%rcx)
jne 0x37f5
xorl %esi, %esi
xorl %edx, %edx
movq %rsi, %r8
movzbl (%rcx,%rsi), %edi
incq %rsi
cmpb 0x1(%rcx,%r8), %dil
jae 0x37e6
movl %edx, 0xc4(%rcx,%rdi,4)
incl %edx
incq %rdi
movzbl (%rcx,%rsi), %r8d
cmpq %r8, %rdi
jb 0x37d0
addl %edx, %edx
cmpq $0x10, %rsi
jne 0x37bf
movb $0x1, 0x384(%rcx)
leaq 0x3594(%rip), %rcx      # 0x6d90
movq (%rcx,%rax,8), %rcx
cmpb $0x0, 0x384(%rcx)
jne 0x3843
xorl %esi, %esi
xorl %edx, %edx
movq %rsi, %r8
movzbl (%rcx,%rsi), %edi
incq %rsi
cmpb 0x1(%rcx,%r8), %dil
jae 0x3834
movl %edx, 0xc4(%rcx,%rdi,4)
incl %edx
incq %rdi
movzbl (%rcx,%rsi), %r8d
cmpq %r8, %rdi
jb 0x381e
addl %edx, %edx
cmpq $0x10, %rsi
jne 0x380d
movb $0x1, 0x384(%rcx)
incq %rax
cmpq $0x3, %rax
jne 0x37a7
cmpl $0x0, 0x10(%r14)
setne %al
je 0x393c
xorl %ebx, %ebx
movb %al, 0x6(%rsp)
cmpl $0x0, 0x14(%r14)
setne %al
je 0x3924
xorl %r13d, %r13d
movb %al, 0x7(%rsp)
xorl %ebp, %ebp
leaq 0x28(%rsp), %r12
xorl %r15d, %r15d
movl 0x14(%r14), %eax
imull %ebx, %eax
addl %r13d, %eax
leaq (%rax,%rax,2), %rsi
shlq $0x8, %rsi
addq 0x8(%r14), %rsi
testl %r15d, %r15d
je 0x38be
cmpl $0x2, %r15d
je 0x38b3
cmpl $0x1, %r15d
jne 0x38bc
addq $0x100, %rsi            # imm = 0x100
jmp 0x38be
addq $0x200, %rsi            # imm = 0x200
jmp 0x38be
xorl %esi, %esi
leaq 0x34ab(%rip), %rax      # 0x6d70
movq (%rbp,%rax), %rcx
leaq 0x34bf(%rip), %rax      # 0x6d90
movq (%rbp,%rax), %r8
leaq 0x38(%rsp), %rdi
movq %r12, %rdx
callq 0x3238
testb %al, %al
je 0x390a
incq %r15
addq $0x4, %r12
addq $0x8, %rbp
cmpq $0x3, %r15
jne 0x3883
incl %r13d
cmpl 0x14(%r14), %r13d
setb %al
jb 0x3875
jmp 0x3924
movq 0x8(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
testb $0x1, 0x7(%rsp)
jne 0x3935
incl %ebx
cmpl 0x10(%r14), %ebx
setb %al
jb 0x3860
jmp 0x393c
testb $0x1, 0x6(%rsp)
jne 0x3963
movaps 0x10(%rsp), %xmm0
movq 0x8(%rsp), %rcx
movups %xmm0, (%rcx)
movq 0x20(%rsp), %rax
movq %rax, 0x10(%rcx)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x397a
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2100
movq 0x8(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x39a8
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2100
movq %rbx, %rdi
callq 0x21c0