// File: /workspace/github_cpp_makefile_star2_O1/misohena[P]drawstars/./image.h
// Function: bool writeBMPFile<Image<ColorRGB<double>>>(char const*, Image<ColorRGB<double>> const&)
bool writeBMPFile(const char *filename, const ImageT &image)
{
    // Header
    const unsigned int fileHeaderSize = 14;
    const unsigned int infoHeaderSize = 40;
    const unsigned int width = image.width();
    const unsigned int height = image.height();
    const unsigned int pixelSize = 3;
    const unsigned int lineSize = pixelSize * width;
    const unsigned int lineStride = (lineSize + 3) & ~3;
    const unsigned int imageSize = lineStride * height;
    const unsigned int offBits = fileHeaderSize + infoHeaderSize;
    const unsigned int fileSize = offBits + imageSize;
#define word(v) (unsigned char)((v)&255), (unsigned char)((v)>>8&255)
#define dword(v) word(v), word(v>>16)
    const unsigned char header[] = {
        // BITMAPFILEHEADER
        'B', 'M', dword(fileSize), 0, 0, 0, 0, dword(offBits),
        // BITMAPINFOHEADER
        dword(infoHeaderSize),
        dword(width), dword(height), word(1), word(8*pixelSize),
        dword(0),
        dword(imageSize),
        dword(0), dword(0), dword(0), dword(0)
    };
#undef dword
#undef word

    // Body
    std::unique_ptr<std::uint8_t[]> body(new std::uint8_t[imageSize]);
    for(int y = 0; y < height; ++y){
        auto dst = body.get() + y * lineStride;
        auto src = image.at(0, height - y - 1);//bottom-up
        for(int x = 0; x < width; ++x, ++src, dst += 3){
            using VT = typename ImageT::PixelType::ValueType;
            dst[0] = static_cast<std::uint8_t>(clamp(255*src->b, VT(0), VT(255)));
            dst[1] = static_cast<std::uint8_t>(clamp(255*src->g, VT(0), VT(255)));
            dst[2] = static_cast<std::uint8_t>(clamp(255*src->r, VT(0), VT(255)));
        }
    }

    if(File file = File(filename, "wb")){
        std::fwrite(header, sizeof(header), 1, file);
        std::fwrite(body.get(), imageSize, 1, file);
        return true;
    }
    else{
        return false;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r15
movq %rdi, 0x40(%rsp)
movl (%rsi), %ebx
movl 0x4(%rsi), %esi
leal (%rbx,%rbx,2), %ebp
addl $0x3, %ebp
andl $-0x4, %ebp
movl %ebp, %edx
imull %esi, %edx
movw $0x4d42, (%rsp)         # imm = 0x4D42
leal 0x36(%rdx), %eax
movb %al, 0x2(%rsp)
movb %ah, 0x3(%rsp)
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0x4(%rsp)
shrl $0x18, %eax
movb %al, 0x5(%rsp)
xorl %r13d, %r13d
movl %r13d, 0x6(%rsp)
movabsq $0x2800000036, %rax     # imm = 0x2800000036
movq %rax, 0xa(%rsp)
movb %bl, 0x12(%rsp)
movb %bh, 0x13(%rsp)
movl %ebx, %eax
shrl $0x10, %eax
movb %al, 0x14(%rsp)
movl %ebx, %eax
shrl $0x18, %eax
movb %al, 0x15(%rsp)
movq %rsi, %rcx
movb %cl, 0x16(%rsp)
movb %ch, 0x17(%rsp)
movl %ecx, %eax
shrl $0x10, %eax
movb %al, 0x18(%rsp)
movl %ecx, %eax
movq %rsi, %r12
shrl $0x18, %eax
movb %al, 0x19(%rsp)
movw $0x1, 0x1a(%rsp)
movb $0x18, 0x1c(%rsp)
movl %r13d, 0x1d(%rsp)
movb $0x0, 0x21(%rsp)
movb %dl, 0x22(%rsp)
movb %dh, 0x23(%rsp)
movl %edx, %eax
shrl $0x10, %eax
movb %al, 0x24(%rsp)
movl %edx, %eax
shrl $0x18, %eax
movb %al, 0x25(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x26(%rsp)
movq %rdx, 0x38(%rsp)
movq %rdx, %rdi
callq 0x4030
movq %rax, %r14
testq %r12, %r12
je 0xa23f
movq %r12, %r11
movq 0x8(%r15), %rax
addq $0x10, %rax
leal -0x1(%r11), %ecx
imull %ebx, %ecx
movsd 0x2ed7(%rip), %xmm0     # 0xd068
xorpd %xmm1, %xmm1
xorl %edx, %edx
testl %ebx, %ebx
je 0xa22e
movl %r13d, %esi
addq %r14, %rsi
movl %ecx, %edi
leaq (%rdi,%rdi,2), %rdi
leaq (%rax,%rdi,8), %rdi
xorl %r8d, %r8d
movl %ebx, %r9d
movsd (%rdi,%r8,8), %xmm2
mulsd %xmm0, %xmm2
movapd %xmm0, %xmm3
minsd %xmm2, %xmm3
cmpltsd %xmm1, %xmm2
andnpd %xmm3, %xmm2
cvttsd2si %xmm2, %r10d
movb %r10b, (%rsi,%r8)
movsd -0x8(%rdi,%r8,8), %xmm2
mulsd %xmm0, %xmm2
movapd %xmm0, %xmm3
minsd %xmm2, %xmm3
cmpltsd %xmm1, %xmm2
andnpd %xmm3, %xmm2
cvttsd2si %xmm2, %r10d
movb %r10b, 0x1(%rsi,%r8)
movsd -0x10(%rdi,%r8,8), %xmm2
mulsd %xmm0, %xmm2
movapd %xmm0, %xmm3
minsd %xmm2, %xmm3
cmpltsd %xmm1, %xmm2
andnpd %xmm3, %xmm2
cvttsd2si %xmm2, %r10d
movb %r10b, 0x2(%rsi,%r8)
addq $0x3, %r8
decl %r9d
jne 0xa1b5
incq %rdx
subl %ebx, %ecx
addl %ebp, %r13d
cmpq %r11, %rdx
jne 0xa197
leaq 0x3302(%rip), %rsi      # 0xd548
movq 0x40(%rsp), %rdi
callq 0x4350
movq %rax, %rbx
testq %rax, %rax
je 0xa282
movq %rsp, %rdi
movl $0x36, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x4580
movl $0x1, %edx
movq %r14, %rdi
movq 0x38(%rsp), %rsi
movq %rbx, %rcx
callq 0x4580
testq %rbx, %rbx
je 0xa28f
movq %rbx, %rdi
callq 0x42c0
testq %rbx, %rbx
setne %bl
movq %r14, %rdi
callq 0x4370
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq