// File: /workspace/github_cpp_makefile_star2_O3/dannye[P]jed/src/decoder.cpp
// Function: writeBMP(JPGImage const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void writeBMP(const JPGImage* const image, const std::string& filename) {
    // 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;
    }

    const uint paddingSize = image->width % 4;
    const uint size = 14 + 12 + image->height * image->width * 3 + paddingSize * image->height;

    byte* buffer = new (std::nothrow) byte[size];
    if (buffer == nullptr) {
        std::cout << "Error - Memory error\n";
        outFile.close();
        return;
    }
    byte* bufferPos = buffer;

    *bufferPos++ = 'B';
    *bufferPos++ = 'M';
    putInt(bufferPos, size);
    putInt(bufferPos, 0);
    putInt(bufferPos, 0x1A);
    putInt(bufferPos, 12);
    putShort(bufferPos, image->width);
    putShort(bufferPos, image->height);
    putShort(bufferPos, 1);
    putShort(bufferPos, 24);

    for (uint y = image->height - 1; y < image->height; --y) {
        const uint blockRow = y / 8;
        const uint pixelRow = y % 8;
        for (uint x = 0; x < image->width; ++x) {
            const uint blockColumn = x / 8;
            const uint pixelColumn = x % 8;
            const uint blockIndex = blockRow * image->blockWidthReal + blockColumn;
            const uint pixelIndex = pixelRow * 8 + pixelColumn;
            *bufferPos++ = image->blocks[blockIndex].b[pixelIndex];
            *bufferPos++ = image->blocks[blockIndex].g[pixelIndex];
            *bufferPos++ = image->blocks[blockIndex].r[pixelIndex];
        }
        for (uint i = 0; i < paddingSize; ++i) {
            *bufferPos++ = 0;
        }
    }

    outFile.write((char*)buffer, size);
    outFile.close();
    delete[] buffer;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq %rsi, %r14
movq %rdi, %rbx
movq 0x41ef(%rip), %r15      # 0x9fd8
leaq 0x1d14(%rip), %rsi      # 0x7b04
movl $0x8, %edx
movq %r15, %rdi
callq 0x2130
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %r15, %rdi
callq 0x2130
leaq 0x1bbb(%rip), %rsi      # 0x79ce
movl $0x4, %edx
movq %rax, %rdi
callq 0x2130
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
movl $0x14, %edx
callq 0x21f0
leaq 0x88(%rsp), %rdi
callq 0x2040
testb %al, %al
je 0x5fdf
movl 0x2068(%rbx), %eax
movl 0x206c(%rbx), %ecx
movl %ecx, %r15d
andl $0x3, %r15d
movq %rcx, %r12
leal (%rcx,%rcx,2), %edi
addl %r15d, %edi
movq %rax, %r14
imull %eax, %edi
addl $0x1a, %edi
movq 0x4145(%rip), %rsi      # 0x9fb8
movq %rdi, %r13
callq 0x21b0
testq %rax, %rax
je 0x5ff9
movw $0x4d42, (%rax)         # imm = 0x4D42
movq %r13, %rdx
movb %dl, 0x2(%rax)
movb %dh, 0x3(%rax)
movl %edx, %ecx
shrl $0x10, %ecx
movb %cl, 0x4(%rax)
movl %edx, %ecx
shrl $0x18, %ecx
movb %cl, 0x5(%rax)
movl $0x0, 0x6(%rax)
movabsq $0xc0000001a, %rcx      # imm = 0xC0000001A
movq %rcx, 0xa(%rax)
movq %r12, %rcx
movb %cl, 0x12(%rax)
movb %ch, 0x13(%rax)
movq %r14, %rcx
movb %cl, 0x14(%rax)
movb %ch, 0x15(%rax)
movq %rax, 0x8(%rsp)
movl $0x180001, 0x16(%rax)   # imm = 0x180001
testl %ecx, %ecx
movq %r13, %rbp
je 0x5fb6
decl %ecx
movq 0x8(%rsp), %r14
addq $0x1a, %r14
andl $0x3, %r12d
leal -0x1(%r15), %eax
incq %rax
movq %rax, 0x10(%rsp)
cmpl $0x0, 0x206c(%rbx)
je 0x5f8b
movl %ecx, %eax
shrl $0x3, %eax
movq %rcx, %r13
leal (,%rcx,8), %ecx
andl $0x38, %ecx
xorl %edx, %edx
movl %edx, %esi
shrl $0x3, %esi
movl %edx, %edi
andl $0x7, %edi
orl %ecx, %edi
movl 0x2098(%rbx), %r8d
imull %eax, %r8d
addl %esi, %r8d
shlq $0x8, %r8
leaq (%r8,%r8,2), %rsi
movq 0x2080(%rbx), %r8
addq %rsi, %r8
shll $0x2, %edi
movb 0x200(%rdi,%r8), %r8b
movb %r8b, (%r14)
movq 0x2080(%rbx), %r8
addq %rsi, %r8
movb 0x100(%rdi,%r8), %r8b
movb %r8b, 0x1(%r14)
addq 0x2080(%rbx), %rsi
movb (%rdi,%rsi), %sil
movb %sil, 0x2(%r14)
addq $0x3, %r14
incl %edx
cmpl 0x206c(%rbx), %edx
jb 0x5f1c
jmp 0x5f8e
movq %rcx, %r13
testl %r15d, %r15d
je 0x5fa5
movq %r14, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x2090
addq 0x10(%rsp), %r14
movq %r13, %rcx
decl %ecx
cmpl 0x2068(%rbx), %ecx
jb 0x5efb
leaq 0x18(%rsp), %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x2170
leaq 0x18(%rsp), %rdi
callq 0x2180
movq %rbx, %rdi
callq 0x2160
jmp 0x601b
movq 0x3ff2(%rip), %rdi      # 0x9fd8
leaq 0x1b20(%rip), %rsi      # 0x7b0d
movl $0x22, %edx
callq 0x2130
jmp 0x601b
movq 0x3fd8(%rip), %rdi      # 0x9fd8
leaq 0x19ee(%rip), %rsi      # 0x79f5
movl $0x15, %edx
callq 0x2130
leaq 0x18(%rsp), %rdi
callq 0x2180
movq 0x3fae(%rip), %rax      # 0x9fd0
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 0x2060
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6063
jmp 0x6063
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x2190
movq %rbx, %rdi
callq 0x21c0