// File: /workspace/github_cpp_makefile_star2_O1/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 $0x88, %rsp
movq %r8, %rbx
movl %ecx, %ebp
movq %rdx, 0x30(%rsp)
movq %rsi, %rdx
movq %rdi, %rsi
leaq 0x18(%rsp), %r15
movq $0x0, (%r15)
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movl $0x803, %ecx            # imm = 0x803
movq %r15, %r8
callq 0x12272
movq (%r12), %r12
testq %r12, %r12
setne %cl
movq (%r15), %rax
testq %rax, %rax
setne %r14b
andb %cl, %r14b
cmpb $0x1, %r14b
jne 0x12213
testl %ebp, %ebp
setg %cl
cmpl %ebp, %eax
setg %al
andb %cl, %al
cmpb $0x1, %al
jne 0x12098
movl %ebp, %eax
movq %rax, 0x18(%rsp)
movb %r14b, 0xf(%rsp)
movl 0x8(%r12), %ebp
movl 0xc(%r12), %edx
movl $0xffffff, %eax         # imm = 0xFFFFFF
movl %ebp, %ecx
andl %eax, %ecx
bswapl %ecx
sarl $0x18, %ebp
orl %ecx, %ebp
andl %edx, %eax
bswapl %eax
sarl $0x18, %edx
orl %eax, %edx
movslq %ebp, %rsi
movl %edx, 0x10(%rsp)
movslq %edx, %rdx
leaq 0x38(%rsp), %rdi
callq 0x19a56
cmpq $0x0, 0x18(%rsp)
je 0x12204
movl 0x10(%rsp), %eax
imull %ebp, %eax
movl %eax, 0x10(%rsp)
testq %rbx, %rbx
js 0x120f7
cvtsi2ss %rbx, %xmm0
jmp 0x1210e
movq %rbx, %rax
shrq %rax
movl %ebx, %ecx
andl $0x1, %ecx
orq %rax, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
movss %xmm0, 0x14(%rsp)
addq $0x10, %r12
movl 0x10(%rsp), %r14d
xorl %r13d, %r13d
leaq 0x60(%rsp), %rbp
movl $0x0, 0x24(%rsp)
leaq 0x38(%rsp), %rdi
leaq 0x24(%rsp), %rsi
callq 0x1abc4
cmpl $0x0, 0x10(%rsp)
je 0x1218c
movq 0x48(%rsp), %rcx
xorl %eax, %eax
movzbl (%r12,%rax), %edx
xorps %xmm1, %xmm1
cvtsi2ss %edx, %xmm1
movaps %xmm1, %xmm0
divss 0x27eab(%rip), %xmm0    # 0x3a00c
testq %rbx, %rbx
je 0x1217c
movss 0x14(%rsp), %xmm0
cmpltss %xmm1, %xmm0
movss 0x27e8f(%rip), %xmm1    # 0x3a008
andps %xmm1, %xmm0
movss %xmm0, (%rcx,%rax,4)
incq %rax
cmpq %rax, %r14
jne 0x1214a
addq %rax, %r12
leaq 0x78(%rsp), %rdi
callq 0xbc7c
movq 0x38(%rsp), %rax
movq 0x40(%rsp), %r15
movq %rax, 0x60(%rsp)
movq %r15, 0x68(%rsp)
imulq %rax, %r15
movq %r15, %rax
shrq $0x3e, %rax
shlq $0x2, %r15
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
orq %r15, %rdi
callq 0x9030
movq %rax, 0x70(%rsp)
movq 0x48(%rsp), %rsi
movq %rax, %rdi
movq %r15, %rdx
callq 0x92b0
movq 0x30(%rsp), %rdi
movq %rbp, %rsi
callq 0x12532
movq %rbp, %rdi
callq 0x19c8e
incq %r13
cmpq 0x18(%rsp), %r13
jne 0x12125
leaq 0x38(%rsp), %rdi
callq 0x19c8e
movb 0xf(%rsp), %r14b
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x12222
callq 0x9420
movl %r14d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x1225a
jmp 0x1224d
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x19c8e
jmp 0x12250
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x19c8e
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x12269
callq 0x9420
movq %rbx, %rdi
callq 0x95d0
nop