// File: /workspace/github_cpp_makefile_star2_O2/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 $0x208, %rsp            # imm = 0x208
movq %rsi, %r14
movq %rdi, %rbx
movq 0x43bd(%rip), %rdi      # 0x8fd8
leaq 0x1e42(%rip), %rsi      # 0x6a64
callq 0x20e0
movq %rax, %rdi
movq %r14, %rsi
callq 0x20d0
leaq 0x1cf5(%rip), %rsi      # 0x692e
movq %rax, %rdi
callq 0x20e0
leaq 0x8(%rsp), %rdi
pushq $0x14
popq %rdx
movq %r14, %rsi
callq 0x21f0
leaq 0x78(%rsp), %rdi
callq 0x2040
testb %al, %al
je 0x4da8
movl 0x2068(%rbx), %eax
movl 0x206c(%rbx), %ecx
movl %ecx, %r14d
andl $0x3, %r14d
movq %rcx, %r13
leal (%rcx,%rcx,2), %edi
addl %r14d, %edi
movl %eax, %r15d
imull %eax, %edi
addl $0x1a, %edi
movq 0x4331(%rip), %rsi      # 0x8fc0
movq %rdi, %r12
callq 0x21c0
testq %rax, %rax
je 0x4dde
movq %rax, %rbp
movw $0x4d42, (%rax)         # imm = 0x4D42
movq %r12, %rdx
movb %dl, 0x2(%rax)
movb %dh, 0x3(%rax)
movl %edx, %eax
shrl $0x10, %eax
movb %al, 0x4(%rbp)
movl %edx, %eax
shrl $0x18, %eax
movb %al, 0x5(%rbp)
andl $0x0, 0x6(%rbp)
movabsq $0xc0000001a, %rax      # imm = 0xC0000001A
movq %rax, 0xa(%rbp)
movq %r13, %rax
movb %al, 0x12(%rbp)
movb %ah, 0x13(%rbp)
movl %r15d, %ecx
movb %cl, 0x14(%rbp)
movb %ch, 0x15(%rbp)
movq %rbp, %rax
addq $0x1a, %rax
movl $0x180001, 0x16(%rbp)   # imm = 0x180001
decl %ecx
cmpl %r15d, %ecx
jae 0x4dbd
movq %rdx, %r11
movl %ecx, %edx
shrl $0x3, %edx
leal (,%rcx,8), %esi
andl $0x38, %esi
xorl %edi, %edi
cmpl 0x206c(%rbx), %edi
jae 0x4d86
movl %edi, %r8d
shrl $0x3, %r8d
movl %edi, %r9d
andl $0x7, %r9d
orl %esi, %r9d
movl 0x2098(%rbx), %r10d
imull %edx, %r10d
addl %r8d, %r10d
imulq $0x300, %r10, %r8       # imm = 0x300
movq 0x2080(%rbx), %r10
addq %r8, %r10
shll $0x2, %r9d
movb 0x200(%r9,%r10), %r10b
movb %r10b, (%rax)
movq 0x2080(%rbx), %r10
addq %r8, %r10
movb 0x100(%r9,%r10), %r10b
movb %r10b, 0x1(%rax)
addq 0x2080(%rbx), %r8
movb (%r9,%r8), %r8b
movb %r8b, 0x2(%rax)
addq $0x3, %rax
incl %edi
jmp 0x4d12
xorl %edx, %edx
cmpl %edx, %r14d
je 0x4d96
movb $0x0, (%rax,%rdx)
incq %rdx
jmp 0x4d88
movl 0x2068(%rbx), %r15d
addq %rdx, %rax
movq %r11, %rdx
jmp 0x4cf3
movq 0x4229(%rip), %rdi      # 0x8fd8
leaq 0x1cb7(%rip), %rsi      # 0x6a6d
callq 0x20e0
jmp 0x4dfb
leaq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x2170
leaq 0x8(%rsp), %rdi
callq 0x2190
movq %rbp, %rdi
callq 0x2150
jmp 0x4dfb
movq 0x41f3(%rip), %rdi      # 0x8fd8
leaq 0x1b69(%rip), %rsi      # 0x6955
callq 0x20e0
leaq 0x8(%rsp), %rdi
callq 0x2190
leaq 0x8(%rsp), %rdi
callq 0x21a0
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4e1b
jmp 0x4e1b
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x21a0
movq %rbx, %rdi
callq 0x21d0