// File: 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:
subq $0x278, %rsp            # imm = 0x278
movq %rdi, 0x270(%rsp)
movq %rsi, 0x268(%rsp)
movq 0x4dba(%rip), %rdi      # 0xbfd8
leaq 0x27fb(%rip), %rsi      # 0x9a20
callq 0x2110
movq %rax, %rdi
movq 0x268(%rsp), %rsi
callq 0x2100
movq %rax, %rdi
leaq 0x26a6(%rip), %rsi      # 0x98ea
callq 0x2110
movq 0x268(%rsp), %rax
movq %rax, 0x8(%rsp)
movl $0x10, %edi
movl $0x4, %esi
callq 0x81d0
movq 0x8(%rsp), %rsi
movl %eax, %edx
leaq 0x68(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x2270
movq 0x10(%rsp), %rdi
callq 0x20c0
movb %al, 0x1b(%rsp)
jmp 0x728b
movb 0x1b(%rsp), %al
testb $0x1, %al
jne 0x72d4
jmp 0x7295
movq 0x4d3c(%rip), %rdi      # 0xbfd8
leaq 0x2786(%rip), %rsi      # 0x9a29
callq 0x2110
jmp 0x72aa
movl $0x1, 0x58(%rsp)
jmp 0x7618
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x60(%rsp)
movl %eax, 0x5c(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x21e0
jmp 0x762a
movq 0x270(%rsp), %rax
movl 0x206c(%rax), %eax
andl $0x3, %eax
movl %eax, 0x54(%rsp)
movq 0x270(%rsp), %rax
movl 0x2068(%rax), %eax
movq 0x270(%rsp), %rcx
imull 0x206c(%rcx), %eax
imull $0x3, %eax, %eax
addl $0x1a, %eax
movl 0x54(%rsp), %ecx
movq 0x270(%rsp), %rdx
imull 0x2068(%rdx), %ecx
addl %ecx, %eax
movl %eax, 0x50(%rsp)
movl 0x50(%rsp), %eax
movl %eax, %edi
movq 0x4c8e(%rip), %rsi      # 0xbfc0
callq 0x2210
movq %rax, 0x48(%rsp)
cmpq $0x0, 0x48(%rsp)
jne 0x7372
movq 0x4c8d(%rip), %rdi      # 0xbfd8
leaq 0x25bf(%rip), %rsi      # 0x9911
callq 0x2110
jmp 0x7359
leaq 0x68(%rsp), %rdi
callq 0x21d0
jmp 0x7365
movl $0x1, 0x58(%rsp)
jmp 0x7618
movq 0x48(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rsp)
movb $0x42, (%rax)
movq 0x40(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rsp)
movb $0x4d, (%rax)
movl 0x50(%rsp), %esi
leaq 0x40(%rsp), %rdi
callq 0x7130
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0x7130
leaq 0x40(%rsp), %rdi
movl $0x1a, %esi
callq 0x7130
leaq 0x40(%rsp), %rdi
movl $0xc, %esi
callq 0x7130
movq 0x270(%rsp), %rax
movl 0x206c(%rax), %esi
leaq 0x40(%rsp), %rdi
callq 0x71b0
movq 0x270(%rsp), %rax
movl 0x2068(%rax), %esi
leaq 0x40(%rsp), %rdi
callq 0x71b0
leaq 0x40(%rsp), %rdi
movl $0x1, %esi
callq 0x71b0
leaq 0x40(%rsp), %rdi
movl $0x18, %esi
callq 0x71b0
movq 0x270(%rsp), %rax
movl 0x2068(%rax), %eax
subl $0x1, %eax
movl %eax, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
movq 0x270(%rsp), %rcx
cmpl 0x2068(%rcx), %eax
jae 0x75d5
movl 0x3c(%rsp), %eax
shrl $0x3, %eax
movl %eax, 0x38(%rsp)
movl 0x3c(%rsp), %eax
andl $0x7, %eax
movl %eax, 0x34(%rsp)
movl $0x0, 0x30(%rsp)
movl 0x30(%rsp), %eax
movq 0x270(%rsp), %rcx
cmpl 0x206c(%rcx), %eax
jae 0x7590
movl 0x30(%rsp), %eax
shrl $0x3, %eax
movl %eax, 0x2c(%rsp)
movl 0x30(%rsp), %eax
andl $0x7, %eax
movl %eax, 0x28(%rsp)
movl 0x38(%rsp), %eax
movq 0x270(%rsp), %rcx
imull 0x2098(%rcx), %eax
addl 0x2c(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x34(%rsp), %eax
shll $0x3, %eax
addl 0x28(%rsp), %eax
movl %eax, 0x20(%rsp)
movq 0x270(%rsp), %rax
movq 0x2080(%rax), %rax
movl 0x24(%rsp), %ecx
imulq $0x300, %rcx, %rcx      # imm = 0x300
addq %rcx, %rax
movl 0x20(%rsp), %ecx
movl 0x200(%rax,%rcx,4), %eax
movb %al, %cl
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movb %cl, (%rax)
movq 0x270(%rsp), %rax
movq 0x2080(%rax), %rax
movl 0x24(%rsp), %ecx
imulq $0x300, %rcx, %rcx      # imm = 0x300
addq %rcx, %rax
movl 0x20(%rsp), %ecx
movl 0x100(%rax,%rcx,4), %eax
movb %al, %cl
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movb %cl, (%rax)
movq 0x270(%rsp), %rax
movq 0x2080(%rax), %rax
movl 0x24(%rsp), %ecx
imulq $0x300, %rcx, %rcx      # imm = 0x300
addq %rcx, %rax
movl 0x20(%rsp), %ecx
movl (%rax,%rcx,4), %eax
movb %al, %cl
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movb %cl, (%rax)
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x7475
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x54(%rsp), %eax
jae 0x75c3
movq 0x40(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rsp)
movb $0x0, (%rax)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x7598
jmp 0x75c5
movl 0x3c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x743f
movq 0x48(%rsp), %rsi
movl 0x50(%rsp), %eax
movl %eax, %edx
leaq 0x68(%rsp), %rdi
callq 0x21a0
jmp 0x75ec
leaq 0x68(%rsp), %rdi
callq 0x21d0
jmp 0x75f8
movq 0x48(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x7610
movq (%rsp), %rdi
callq 0x2190
movl $0x0, 0x58(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x21e0
addq $0x278, %rsp            # imm = 0x278
retq
movq 0x60(%rsp), %rdi
callq 0x2220
nopw %cs:(%rax,%rax)