// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %rsi, %r15
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)
andl $0x0, 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)
movq %rbx, %r13
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, %r14
shrl $0x18, %eax
movb %al, 0x19(%rsp)
movw $0x1, 0x1a(%rsp)
movb $0x18, 0x1c(%rsp)
andl $0x0, 0x1d(%rsp)
movq %rdi, 0x40(%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)
xorps %xmm0, %xmm0
movups %xmm0, 0x26(%rsp)
movq %rdx, 0x38(%rsp)
movq %rdx, %rdi
callq 0x4030
movq %rax, %rbx
movq %rax, 0x50(%rsp)
movq 0x8(%r15), %rax
movl (%r15), %ecx
addq $0x10, %rax
movq %r14, %r12
leal -0x1(%r14), %edx
imull %ecx, %edx
xorl %esi, %esi
movsd 0x39e6(%rip), %xmm0     # 0xc068
xorpd %xmm1, %xmm1
xorl %edi, %edi
cmpq %r12, %rdi
je 0x872f
movl %esi, %r8d
addq %rbx, %r8
movl %edx, %r9d
imulq $0x18, %r9, %r9
addq %rax, %r9
movq %r13, %r10
xorl %r11d, %r11d
subl $0x1, %r10d
jb 0x8723
movsd (%r9,%r11,8), %xmm2
mulsd %xmm0, %xmm2
movapd %xmm0, %xmm3
minsd %xmm2, %xmm3
cmpltsd %xmm1, %xmm2
andnpd %xmm3, %xmm2
cvttsd2si %xmm2, %r15d
movb %r15b, (%r8,%r11)
movsd -0x8(%r9,%r11,8), %xmm2
mulsd %xmm0, %xmm2
movapd %xmm0, %xmm3
minsd %xmm2, %xmm3
cmpltsd %xmm1, %xmm2
andnpd %xmm3, %xmm2
cvttsd2si %xmm2, %r15d
movb %r15b, 0x1(%r8,%r11)
movsd -0x10(%r9,%r11,8), %xmm2
mulsd %xmm0, %xmm2
movapd %xmm0, %xmm3
minsd %xmm2, %xmm3
cmpltsd %xmm1, %xmm2
andnpd %xmm3, %xmm2
cvttsd2si %xmm2, %r15d
movb %r15b, 0x2(%r8,%r11)
addq $0x3, %r11
jmp 0x86a7
incq %rdi
addl %ebp, %esi
subl %ecx, %edx
jmp 0x8688
leaq 0x3e02(%rip), %rsi      # 0xc538
movq 0x40(%rsp), %rdi
callq 0x4360
movq %rax, %r14
movq %rax, 0x48(%rsp)
testq %rax, %rax
je 0x8775
movq %rsp, %rdi
pushq $0x36
popq %rsi
pushq $0x1
popq %r15
movq %r15, %rdx
movq %r14, %rcx
callq 0x4590
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x4590
testq %r14, %r14
setne %bl
leaq 0x48(%rsp), %rdi
callq 0x94cc
leaq 0x50(%rsp), %rdi
callq 0x94b2
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq