// File: /workspace/github_cpp_makefile_star2_O3/btbujiangjun[P]abcdl/include/utils/MnistHelper.h
// Function: abcdl::utils::MnistHelper<float>::read_image(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, abcdl::algebra::Matrix<float>*, int, unsigned long)
bool MnistHelper<T>::read_image(const std::string& image_file,
                                abcdl::algebra::Matrix<T>* out_mat,
                                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);

    size_t size = count * rows * cols;
    T* data = new T[size];
    for(size_t i = 0; i < size; i++){
        if(threshold > 0){
            data[i] = static_cast<T>(*image_data_buffer++) > threshold ? 1 : 0;
        }else{
            data[i] = static_cast<T>(*image_data_buffer++) / 255;
        }
    }

    out_mat->set_shallow_data(data, count, rows * cols);

    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %r15
movl %ecx, %ebp
movq %rdx, %r14
movq %rsi, %rdx
movq %rdi, %rsi
leaq 0x20(%rsp), %rbx
movq $0x0, (%rbx)
leaq 0x8(%rsp), %r12
movq %r12, %rdi
movl $0x803, %ecx            # imm = 0x803
movq %rbx, %r8
callq 0x11f4c
movq (%r12), %r12
testq %r12, %r12
setne %al
movq (%rbx), %rdx
testq %rdx, %rdx
setne %bl
andb %al, %bl
cmpb $0x1, %bl
jne 0x148b6
movq %r14, 0x18(%rsp)
testl %ebp, %ebp
setg %al
cmpl %ebp, %edx
setg %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x147d4
movl %ebp, %edx
movq %rdx, 0x20(%rsp)
movl 0x8(%r12), %ebp
movl 0xc(%r12), %r14d
movl $0xffffff, %eax         # imm = 0xFFFFFF
movl %ebp, %ecx
andl %eax, %ecx
bswapl %ecx
sarl $0x18, %ebp
orl %ecx, %ebp
andl %r14d, %eax
bswapl %eax
sarl $0x18, %r14d
orl %eax, %r14d
movslq %ebp, %rax
movq %rdx, 0x10(%rsp)
imulq %rdx, %rax
movslq %r14d, %r13
imulq %rax, %r13
movq %r13, %rax
shrq $0x3e, %rax
leaq (,%r13,4), %rcx
xorl %edi, %edi
negq %rax
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x9030
testq %r13, %r13
je 0x14898
testq %r15, %r15
js 0x1483d
cvtsi2ss %r15, %xmm0
jmp 0x14855
movq %r15, %rcx
shrq %rcx
movl %r15d, %edx
andl $0x1, %edx
orq %rcx, %rdx
cvtsi2ss %rdx, %xmm0
addss %xmm0, %xmm0
xorl %ecx, %ecx
movss 0x257a9(%rip), %xmm1    # 0x3a008
movss 0x257a5(%rip), %xmm2    # 0x3a00c
movzbl 0x10(%r12,%rcx), %edx
xorps %xmm4, %xmm4
cvtsi2ss %edx, %xmm4
movaps %xmm4, %xmm3
divss %xmm1, %xmm3
testq %r15, %r15
je 0x1488b
movaps %xmm0, %xmm3
cmpltss %xmm4, %xmm3
andps %xmm2, %xmm3
movss %xmm3, (%rax,%rcx,4)
incq %rcx
cmpq %rcx, %r13
jne 0x14867
imull %ebp, %r14d
movslq %r14d, %rcx
movq 0x18(%rsp), %rdi
movq %rax, %rsi
movq 0x10(%rsp), %rdx
callq 0x19db2
movq 0x8(%rsp), %r12
testq %r12, %r12
je 0x148c3
movq %r12, %rdi
callq 0x9420
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x148e6
callq 0x9420
movq %rbx, %rdi
callq 0x95d0