// File: /workspace/github_cpp_makefile_star2_O1/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 0x3769
movq (%r14), %rbx
movq 0x8(%r14), %r14
subq %rbx, %r14
je 0x3f32
movq 0x3253(%rip), %rdi      # 0x6fd8
leaq 0x14c1(%rip), %rsi      # 0x524d
movl $0x8, %edx
callq 0x2120
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq 0x3232(%rip), %rdi      # 0x6fd8
callq 0x2120
leaq 0x1327(%rip), %rsi      # 0x50d9
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 0x3ee8
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 0x3c75
leaq 0x151b(%rip), %rdx      # 0x5330
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x39d8
leaq 0x160c(%rip), %rdx      # 0x5434
leaq 0x18(%rsp), %rdi
movl $0x1, %esi
callq 0x39d8
leaq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x3a4f
leaq 0x32cd(%rip), %rcx      # 0x7118
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3b1e
leaq 0x3640(%rip), %rcx      # 0x74a0
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x3b1e
leaq 0x39b0(%rip), %rcx      # 0x7828
leaq 0x18(%rsp), %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x3b1e
leaq 0x3d20(%rip), %rcx      # 0x7bb0
leaq 0x18(%rsp), %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0x3b1e
leaq 0x18(%rsp), %rdi
callq 0x3bde
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 0x3f00
movq 0x30e9(%rip), %rdi      # 0x6fd8
leaq 0x1360(%rip), %rsi      # 0x5256
movl $0x22, %edx
callq 0x2120
movq 0x30c9(%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 0x3f47
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 0x3f58
movq %rax, %rbx
jmp 0x3f6a
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x2170
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x3f80
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2100
movq %rbx, %rdi
callq 0x21c0