// File: /workspace/github_cpp_makefile_star2_O1/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 0x3f03(%rip), %r15      # 0x9fd8
leaq 0x1a28(%rip), %rsi      # 0x7b04
movl $0x8, %edx
movq %r15, %rdi
callq 0x2130
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %r15, %rdi
callq 0x2130
leaq 0x18cf(%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 0x62d1
movl 0x206c(%rbx), %eax
movl %eax, %r12d
andl $0x3, %r12d
movq %rax, 0x8(%rsp)
leal (%rax,%rax,2), %edi
addl %r12d, %edi
imull 0x2068(%rbx), %edi
addl $0x1a, %edi
movq 0x3e5c(%rip), %rsi      # 0x9fb8
movq %rdi, %r14
callq 0x21b0
testq %rax, %rax
je 0x62eb
movw $0x4d42, (%rax)         # imm = 0x4D42
movq %r14, %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)
movzwl 0x206c(%rbx), %ecx
movw %cx, 0x12(%rax)
movl 0x2068(%rbx), %ecx
movb %cl, 0x14(%rax)
movb %ch, 0x15(%rax)
movq %rax, 0x10(%rsp)
movl $0x180001, 0x16(%rax)   # imm = 0x180001
movl 0x2068(%rbx), %r13d
testl %r13d, %r13d
je 0x62a8
decl %r13d
movq 0x10(%rsp), %r15
addq $0x1a, %r15
movq 0x8(%rsp), %rax
andl $0x3, %eax
movq %rax, 0x8(%rsp)
leal -0x1(%r12), %ebp
incq %rbp
cmpl $0x0, 0x206c(%rbx)
je 0x6281
movl %r13d, %eax
shrl $0x3, %eax
leal (,%r13,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, (%r15)
movq 0x2080(%rbx), %r8
addq %rsi, %r8
movb 0x100(%rdi,%r8), %r8b
movb %r8b, 0x1(%r15)
addq 0x2080(%rbx), %rsi
movb (%rdi,%rsi), %sil
movb %sil, 0x2(%r15)
addq $0x3, %r15
incl %edx
cmpl 0x206c(%rbx), %edx
jb 0x6214
testl %r12d, %r12d
je 0x6298
movq %r15, %rdi
xorl %esi, %esi
movq 0x8(%rsp), %rdx
callq 0x2090
addq %rbp, %r15
decl %r13d
cmpl 0x2068(%rbx), %r13d
jb 0x61f4
leaq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2170
leaq 0x18(%rsp), %rdi
callq 0x2180
movq %rbx, %rdi
callq 0x2160
jmp 0x630d
movq 0x3d00(%rip), %rdi      # 0x9fd8
leaq 0x182e(%rip), %rsi      # 0x7b0d
movl $0x22, %edx
callq 0x2130
jmp 0x630d
movq 0x3ce6(%rip), %rdi      # 0x9fd8
leaq 0x16fc(%rip), %rsi      # 0x79f5
movl $0x15, %edx
callq 0x2130
leaq 0x18(%rsp), %rdi
callq 0x2180
movq 0x3cbc(%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 0x6355
jmp 0x6355
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x2190
movq %rbx, %rdi
callq 0x21c0