// File: /workspace/github_cpp_makefile_star2_O3/dannye[P]jed/src/encoder.cpp
// Function: writeJPG(BMPImage const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void writeJPG(const BMPImage& image, const std::string& filename) {
    std::vector<byte> huffmanData = encodeHuffmanData(image);
    if (huffmanData.size() == 0) {
        return;
    }

    // open file
    std::cout << "Writing " << filename << "...\n";
    std::ofstream outFile(filename, std::ios::out | std::ios::binary);
    if (!outFile.is_open()) {
        std::cout << "Error - Error opening output file\n";
        return;
    }

    // SOI
    outFile.put(0xFF);
    outFile.put(SOI);

    // APP0
    writeAPP0(outFile);

    // DQT
    writeQuantizationTable(outFile, 0, qTableY100);
    writeQuantizationTable(outFile, 1, qTableCbCr100);

    // SOF
    writeStartOfFrame(outFile, image);

    // DHT
    writeHuffmanTable(outFile, 0, 0, hDCTableY);
    writeHuffmanTable(outFile, 0, 1, hDCTableCbCr);
    writeHuffmanTable(outFile, 1, 0, hACTableY);
    writeHuffmanTable(outFile, 1, 1, hACTableCbCr);

    // SOS
    writeStartOfScan(outFile);

    // ECS
    outFile.write((char*)&huffmanData[0], huffmanData.size());

    // EOI
    outFile.put(0xFF);
    outFile.put(EOI);

    outFile.close();
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq %rsi, %r12
movq %rdi, %r15
movq %rsp, %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x34e4
movq (%r14), %rbx
movq 0x8(%r14), %r14
subq %rbx, %r14
je 0x3cb1
movq 0x34d4(%rip), %rdi      # 0x6fd8
leaq 0x1732(%rip), %rsi      # 0x523d
movl $0x8, %edx
callq 0x2120
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq 0x34b3(%rip), %rdi      # 0x6fd8
callq 0x2120
leaq 0x1598(%rip), %rsi      # 0x50c9
movl $0x4, %edx
movq %rax, %rdi
callq 0x2120
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
movl $0x14, %edx
callq 0x21f0
leaq 0x88(%rsp), %rdi
callq 0x2050
testb %al, %al
je 0x3c67
leaq 0x18(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x2030
leaq 0x18(%rsp), %rdi
movl $0xffffffd8, %esi       # imm = 0xFFFFFFD8
callq 0x2030
leaq 0x18(%rsp), %rdi
callq 0x39f4
leaq 0x178c(%rip), %rdx      # 0x5320
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x3755
leaq 0x187d(%rip), %rdx      # 0x5424
leaq 0x18(%rsp), %rdi
movl $0x1, %esi
callq 0x3755
leaq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x37cc
leaq 0x354e(%rip), %rcx      # 0x7118
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x389b
leaq 0x38c1(%rip), %rcx      # 0x74a0
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x389b
leaq 0x3c31(%rip), %rcx      # 0x7828
leaq 0x18(%rsp), %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x389b
leaq 0x3fa1(%rip), %rcx      # 0x7bb0
leaq 0x18(%rsp), %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0x389b
leaq 0x18(%rsp), %rdi
callq 0x395d
leaq 0x18(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2150
leaq 0x18(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x2030
leaq 0x18(%rsp), %rdi
movl $0xffffffd9, %esi       # imm = 0xFFFFFFD9
callq 0x2030
leaq 0x18(%rsp), %rdi
callq 0x2160
jmp 0x3c7f
movq 0x336a(%rip), %rdi      # 0x6fd8
leaq 0x15d1(%rip), %rsi      # 0x5246
movl $0x22, %edx
callq 0x2120
movq 0x334a(%rip), %rax      # 0x6fd0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x20(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x18(%rsp,%rcx)
callq 0x20d0
leaq 0x110(%rsp), %rdi
callq 0x2070
testq %rbx, %rbx
je 0x3cc6
movq 0x10(%rsp), %rsi
subq %rbx, %rsi
movq %rbx, %rdi
callq 0x2100
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r14
popq %r15
retq
jmp 0x3cd7
movq %rax, %r14
jmp 0x3ce9
movq %rax, %r14
leaq 0x18(%rsp), %rdi
callq 0x2170
testq %rbx, %rbx
je 0x3cfe
movq 0x10(%rsp), %rsi
subq %rbx, %rsi
movq %rbx, %rdi
callq 0x2100
movq %r14, %rdi
callq 0x21c0