// File: /workspace/github_c_makefile_star2_O0/stonezwr[P]SpiKL-IP/util.C
// Function: ReadMnistSample(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::vector<std::vector<std::vector<float, std::allocator<float>>, std::allocator<std::vector<float, std::allocator<float>>>>, std::allocator<std::vector<std::vector<float, std::allocator<float>>, std::allocator<std::vector<float, std::allocator<float>>>>>>&, int)
int ReadMnistSample(std::string filename, Matrices& vec, int num)
{
    std::ifstream file(filename.c_str(), std::ios::binary);
    if(!file.is_open()){
        std::cout<<"Cannot open the file: "<<filename<<std::endl;
        exit(EXIT_FAILURE);
    }
    int magic_number = 0;
    int number_of_images = 0;
    int n_rows = 0;
    int n_cols = 0;
    file.read((char*) &magic_number, sizeof(magic_number));
    magic_number = ReverseInt(magic_number);
    file.read((char*) &number_of_images,sizeof(number_of_images));
    number_of_images = ReverseInt(number_of_images);
    if(number_of_images >= num){
        number_of_images = num;
    }
    else{
        std::cout<<"ReadMnistSample::number of images "<<num<<" is overflow!"<<std::endl;
        exit(EXIT_FAILURE);
    }
    file.read((char*) &n_rows, sizeof(n_rows));
    n_rows = ReverseInt(n_rows);
    file.read((char*) &n_cols, sizeof(n_cols));
    n_cols = ReverseInt(n_cols);
    for(int i = 0; i < number_of_images; ++i){
        Matrix tpmat = Matrix(n_rows, std::vector<float>(n_cols, 1));
                    
        for(int r = 0; r < n_rows; ++r){
            for(int c = 0; c < n_cols; ++c){
                unsigned char temp = 0;
                file.read((char*) &temp, sizeof(temp));        
                tpmat[r][c] = (float)temp / (5.5*255.0f);
                //if(i == 0)  std::cout<<tpmat[r][c]<<"\t";
            }
            //if(i == 0)  std::cout<<std::endl;
        }
        vec.push_back(tpmat);
    }
    return vec.size();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2f0, %rsp            # imm = 0x2F0
movq %rdi, -0x298(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
callq 0x4160
movq %rax, %rsi
leaq -0x220(%rbp), %rdi
movq %rdi, -0x290(%rbp)
movl $0x4, %edx
callq 0x4750
movq -0x290(%rbp), %rdi
callq 0x4620
movb %al, -0x282(%rbp)
jmp 0x2e901
movb -0x282(%rbp), %al
testb $0x1, %al
jne 0x2e97b
jmp 0x2e90d
movq 0x1e6b4(%rip), %rdi     # 0x4cfc8
leaq 0x826f(%rip), %rsi      # 0x36b8a
callq 0x4360
movq %rax, -0x2a0(%rbp)
jmp 0x2e929
movq -0x298(%rbp), %rsi
movq -0x2a0(%rbp), %rdi
callq 0x4300
movq %rax, -0x2a8(%rbp)
jmp 0x2e945
movq -0x2a8(%rbp), %rdi
movq 0x1e665(%rip), %rsi     # 0x4cfb8
callq 0x43b0
jmp 0x2e95a
movl $0x1, %edi
callq 0x4440
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
jmp 0x2ed49
movl $0x0, -0x230(%rbp)
movl $0x0, -0x234(%rbp)
movl $0x0, -0x238(%rbp)
movl $0x0, -0x23c(%rbp)
leaq -0x220(%rbp), %rdi
leaq -0x230(%rbp), %rsi
movl $0x4, %edx
callq 0x44b0
jmp 0x2e9bd
movl -0x230(%rbp), %edi
callq 0x2e850
movl %eax, -0x230(%rbp)
leaq -0x220(%rbp), %rdi
leaq -0x234(%rbp), %rsi
movl $0x4, %edx
callq 0x44b0
jmp 0x2e9e8
movl -0x234(%rbp), %edi
callq 0x2e850
movl %eax, -0x234(%rbp)
movl -0x234(%rbp), %eax
cmpl -0x14(%rbp), %eax
jl 0x2ea0f
movl -0x14(%rbp), %eax
movl %eax, -0x234(%rbp)
jmp 0x2ea7e
movq 0x1e5b2(%rip), %rdi     # 0x4cfc8
leaq 0x8184(%rip), %rsi      # 0x36ba1
callq 0x4360
movq %rax, -0x2b0(%rbp)
jmp 0x2ea2b
movq -0x2b0(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x46c0
movq %rax, -0x2b8(%rbp)
jmp 0x2ea43
movq -0x2b8(%rbp), %rdi
leaq 0x8173(%rip), %rsi      # 0x36bc4
callq 0x4360
movq %rax, -0x2c0(%rbp)
jmp 0x2ea5f
movq -0x2c0(%rbp), %rdi
movq 0x1e54b(%rip), %rsi     # 0x4cfb8
callq 0x43b0
jmp 0x2ea74
movl $0x1, %edi
callq 0x4440
leaq -0x220(%rbp), %rdi
leaq -0x238(%rbp), %rsi
movl $0x4, %edx
callq 0x44b0
jmp 0x2ea98
movl -0x238(%rbp), %edi
callq 0x2e850
movl %eax, -0x238(%rbp)
leaq -0x220(%rbp), %rdi
leaq -0x23c(%rbp), %rsi
movl $0x4, %edx
callq 0x44b0
jmp 0x2eac3
movl -0x23c(%rbp), %edi
callq 0x2e850
movl %eax, -0x23c(%rbp)
movl $0x0, -0x240(%rbp)
movl -0x240(%rbp), %eax
cmpl -0x234(%rbp), %eax
jge 0x2ed1f
movslq -0x238(%rbp), %rax
movq %rax, -0x2d8(%rbp)
movslq -0x23c(%rbp), %rax
movq %rax, -0x2d0(%rbp)
movl $0x3f800000, -0x274(%rbp) # imm = 0x3F800000
leaq -0x275(%rbp), %rdi
movq %rdi, -0x2c8(%rbp)
callq 0x2f320
movq -0x2d0(%rbp), %rsi
movq -0x2c8(%rbp), %rcx
leaq -0x270(%rbp), %rdi
leaq -0x274(%rbp), %rdx
callq 0x2f340
jmp 0x2eb4c
leaq -0x276(%rbp), %rdi
movq %rdi, -0x2e0(%rbp)
callq 0x2f3c0
movq -0x2d8(%rbp), %rsi
movq -0x2e0(%rbp), %rcx
leaq -0x258(%rbp), %rdi
leaq -0x270(%rbp), %rdx
callq 0x2f3e0
jmp 0x2eb82
leaq -0x276(%rbp), %rdi
callq 0x2f460
leaq -0x270(%rbp), %rdi
callq 0x2f480
leaq -0x275(%rbp), %rdi
callq 0x2f4e0
movl $0x0, -0x27c(%rbp)
movl -0x27c(%rbp), %eax
cmpl -0x238(%rbp), %eax
jge 0x2eced
movl $0x0, -0x280(%rbp)
movl -0x280(%rbp), %eax
cmpl -0x23c(%rbp), %eax
jge 0x2ecd7
movb $0x0, -0x281(%rbp)
leaq -0x220(%rbp), %rdi
leaq -0x281(%rbp), %rsi
movl $0x1, %edx
callq 0x44b0
jmp 0x2ebff
movzbl -0x281(%rbp), %eax
cvtsi2ss %eax, %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x7df2(%rip), %xmm1     # 0x36a08
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x2e4(%rbp)
movslq -0x27c(%rbp), %rsi
leaq -0x258(%rbp), %rdi
callq 0x2f500
movq %rax, %rdi
movslq -0x280(%rbp), %rsi
callq 0x2f520
movss -0x2e4(%rbp), %xmm0
movss %xmm0, (%rax)
movl -0x280(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x280(%rbp)
jmp 0x2ebcc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
jmp 0x2eca6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
leaq -0x276(%rbp), %rdi
callq 0x2f460
leaq -0x270(%rbp), %rdi
callq 0x2f480
leaq -0x275(%rbp), %rdi
callq 0x2f4e0
jmp 0x2ed49
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
leaq -0x258(%rbp), %rdi
callq 0x2f5b0
jmp 0x2ed49
jmp 0x2ecd9
movl -0x27c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x27c(%rbp)
jmp 0x2ebb0
movq -0x10(%rbp), %rdi
leaq -0x258(%rbp), %rsi
callq 0x2f540
jmp 0x2ecff
leaq -0x258(%rbp), %rdi
callq 0x2f5b0
movl -0x240(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x240(%rbp)
jmp 0x2eade
movq -0x10(%rbp), %rdi
callq 0x2f610
movl %eax, -0x2e8(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x40f0
movl -0x2e8(%rbp), %eax
addq $0x2f0, %rsp            # imm = 0x2F0
popq %rbp
retq
leaq -0x220(%rbp), %rdi
callq 0x40f0
movq -0x228(%rbp), %rdi
callq 0x46d0
nopw %cs:(%rax,%rax)