// File: /workspace/github_cpp_makefile_star2_O2/btbujiangjun[P]abcdl/include/utils/MnistHelper.h
// Function: abcdl::utils::MnistHelper<float>::read_images(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, abcdl::algebra::MatrixSet<float>&, int, unsigned long)
bool MnistHelper<T>::read_images(const std::string& image_file,
                                 abcdl::algebra::MatrixSet<T>& out_matrix_set,
                                 const int limit,
                                 const size_t threshold){

    size_t count = 0;
    auto image_buffer = read_mnist_file(image_file, 0x803, &count);

    if(!image_buffer || count == 0){
        return false;
    }

    if( limit > 0 && limit < (int)count){
        count = limit;
    }

    //read image data
    auto rows = read_header(image_buffer, 2);
    auto cols = read_header(image_buffer, 3);

    auto image_data_buffer = reinterpret_cast<unsigned char*>(image_buffer.get() + 16);

    auto size = rows * cols;
    abcdl::algebra::Matrix<T> mat(rows, cols);
    for(size_t i = 0; i != count; i++){
        mat.reset();
        T* data = mat.data();
        for(int j = 0; j != size; j++){
            if(threshold > 0){
                data[j] = static_cast<T>(*image_data_buffer++) > threshold ? 1 : 0;
            }else{
                data[j] = static_cast<T>(*image_data_buffer++) / 255;
            }
        }
        out_matrix_set.push_back(mat);
    }

    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r8, %rbx
movl %ecx, %r15d
movq %rdx, 0x20(%rsp)
movq %rsi, %rdx
movq %rdi, %rsi
leaq 0x18(%rsp), %r12
andq $0x0, (%r12)
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movl $0x803, %ecx            # imm = 0x803
movq %r12, %r8
callq 0x10f68
movq (%r13), %rbp
testq %rbp, %rbp
setne %cl
movq (%r12), %rax
testq %rax, %rax
setne %r13b
andb %cl, %r13b
cmpb $0x1, %r13b
jne 0x10ea0
testl %r15d, %r15d
setg %cl
cmpl %r15d, %eax
setg %al
andb %cl, %al
cmpb $0x1, %al
jne 0x10d88
movl %r15d, %eax
movq %rax, 0x18(%rsp)
movl 0x8(%rbp), %r14d
movl 0xc(%rbp), %r12d
movl $0xffffff, %eax         # imm = 0xFFFFFF
movl %r14d, %ecx
andl %eax, %ecx
bswapl %ecx
sarl $0x18, %r14d
orl %ecx, %r14d
andl %r12d, %eax
bswapl %eax
sarl $0x18, %r12d
orl %eax, %r12d
movslq %r14d, %rsi
movslq %r12d, %rdx
leaq 0x28(%rsp), %rdi
callq 0x17dc6
imull %r14d, %r12d
testq %rbx, %rbx
movb %r13b, 0x7(%rsp)
js 0x10dd4
cvtsi2ss %rbx, %xmm0
jmp 0x10deb
movq %rbx, %rax
shrq %rax
movl %ebx, %ecx
andl $0x1, %ecx
orq %rax, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
addq $0x10, %rbp
xorl %r14d, %r14d
leaq 0x50(%rsp), %r13
cmpq 0x18(%rsp), %r14
je 0x10e91
andl $0x0, 0xc(%rsp)
leaq 0x28(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x18c44
movq 0x38(%rsp), %rax
xorl %r15d, %r15d
cmpq %r15, %r12
je 0x10e64
movzbl (%rbp,%r15), %ecx
cvtsi2ss %ecx, %xmm1
movaps %xmm1, %xmm0
divss 0x1f1ca(%rip), %xmm0    # 0x30008
testq %rbx, %rbx
je 0x10e59
movss 0x8(%rsp), %xmm0
cmpltss %xmm1, %xmm0
movss 0x1f1b6(%rip), %xmm1    # 0x3000c
andps %xmm1, %xmm0
movss %xmm0, (%rax,%r15,4)
incq %r15
jmp 0x10e24
movq %r13, %rdi
leaq 0x28(%rsp), %rsi
callq 0x11164
movq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x111d8
addq %r15, %rbp
movq %r13, %rdi
callq 0x17fd6
incq %r14
jmp 0x10dfd
leaq 0x28(%rsp), %rdi
callq 0x17fd6
movb 0x7(%rsp), %r13b
leaq 0x10(%rsp), %rdi
callq 0x111be
movl %r13d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x10edf
jmp 0x10ec3
movq %rax, %rbx
jmp 0x10ed5
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x17fd6
leaq 0x28(%rsp), %rdi
callq 0x17fd6
leaq 0x10(%rsp), %rdi
callq 0x111be
movq %rbx, %rdi
callq 0x95e0