// File: /workspace/github_cpp_makefile_star2_O2/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 %rbx
subq $0x220, %rsp            # imm = 0x220
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x8(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3105
movq 0x8(%r15), %rax
cmpq (%r15), %rax
je 0x371f
movq 0x3a0f(%rip), %rdi      # 0x6fd0
leaq 0xc0d(%rip), %rsi       # 0x41d5
callq 0x2100
movq %rax, %rdi
movq %r14, %rsi
callq 0x20f0
leaq 0xa82(%rip), %rsi       # 0x4061
movq %rax, %rdi
callq 0x2100
leaq 0x20(%rsp), %rdi
pushq $0x14
popq %rdx
movq %r14, %rsi
callq 0x2210
leaq 0x90(%rsp), %rdi
callq 0x2050
testb %al, %al
je 0x3702
leaq 0x20(%rsp), %rdi
pushq $-0x1
popq %rsi
callq 0x2030
leaq 0x20(%rsp), %rdi
pushq $-0x28
popq %rsi
callq 0x2030
leaq 0x20(%rsp), %rdi
callq 0x34df
leaq 0xc59(%rip), %rdx       # 0x4290
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x32ba
leaq 0xd4a(%rip), %rdx       # 0x4394
leaq 0x20(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x32ba
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x3323
leaq 0x3abd(%rip), %rcx      # 0x7128
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x33b8
leaq 0x3e30(%rip), %rcx      # 0x74b0
leaq 0x20(%rsp), %rdi
pushq $0x1
popq %rdx
xorl %esi, %esi
callq 0x33b8
leaq 0x41a2(%rip), %rcx      # 0x7838
leaq 0x20(%rsp), %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x33b8
leaq 0x4514(%rip), %rcx      # 0x7bc0
leaq 0x20(%rsp), %rdi
pushq $0x1
popq %rdx
movl %edx, %esi
callq 0x33b8
leaq 0x20(%rsp), %rdi
callq 0x344e
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
subq %rsi, %rdx
leaq 0x20(%rsp), %rdi
callq 0x2170
leaq 0x20(%rsp), %rdi
pushq $-0x1
popq %rsi
callq 0x2030
leaq 0x20(%rsp), %rdi
pushq $-0x27
popq %rsi
callq 0x2030
leaq 0x20(%rsp), %rdi
callq 0x2190
jmp 0x3715
movq 0x38c7(%rip), %rdi      # 0x6fd0
leaq 0xace(%rip), %rsi       # 0x41de
callq 0x2100
leaq 0x20(%rsp), %rdi
callq 0x21a0
leaq 0x8(%rsp), %rdi
callq 0x3b20
addq $0x220, %rsp            # imm = 0x220
popq %rbx
popq %r14
popq %r15
retq
jmp 0x3738
movq %rax, %rbx
jmp 0x374a
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x21a0
leaq 0x8(%rsp), %rdi
callq 0x3b20
movq %rbx, %rdi
callq 0x21f0