// File: /workspace/github_cpp_makefile_star2_O2/btbujiangjun[P]abcdl/include/utils/ModelLoader.h
// Function: bool abcdl::utils::ModelLoader::write<float>(std::vector<abcdl::algebra::Matrix<float>*, std::allocator<abcdl::algebra::Matrix<float>*>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool)
bool ModelLoader::write(std::vector<abcdl::algebra::Matrix<T>*> models,
                        const std::string& path,
                        const std::string& signature,
                        bool is_append){
    std::vector<abcdl::algebra::Matrix<T>*> old_models;
    if(is_append){
        read<T>(path, &old_models, signature);
    }
    
    size_t num_models = models.size() + old_models.size();
    std::vector<ModelInfo> infos;

    if(!generate_header(old_models, &infos) || !generate_header(models, &infos)){
        return false;
    }
     
    /**
    std::ofstream out_file(path, std::ios::binary);
    std::ofstream out_file(path, std::ios::out);

    out_file.write(signature.c_str(), sizeof(char)*signature.size());
    out_file.write((char*)&num_models, sizeof(size_t));
    for(auto&& info : infos){
        out_file.write(&info.type, sizeof(char));
        out_file.write((char*)&info.rows, sizeof(size_t));
        out_file.write((char*)&info.cols, sizeof(size_t));
    }

    size_t num_old_models = old_models.size();
    for(size_t i = 0; i != num_old_models; i++){
        out_file.write((char*)old_models[i]->data(), sizeof(T) * infos[i].rows * infos[i].cols);
    }

    num_models = models.size();
    for(size_t i = 0; i != num_models; i++){
        ModelInfo info = infos[i+num_old_models];
        out_file.write((char*)models[i]->data(), sizeof(T) * info.rows * info.cols);
    }
    **/

    std::ofstream out_file(path, std::ios::out);
    out_file << signature << std::endl;
    out_file << num_models << std::endl;
    for(auto&& info : infos){
        out_file << info.type << std::endl;
        out_file << info.rows << std::endl;
        out_file << info.cols << std::endl;
    }
    
    size_t num_old_models = old_models.size();
    for(size_t i = 0; i != num_old_models; i++){
        size_t model_size = infos[i].rows * infos[i].cols;
        for(size_t j = 0; j < model_size; j++){
            out_file << old_models[i]->get_data(j) << std::endl;
        }
    }

    num_models = models.size();
    for(size_t i = 0; i < num_models; i++){
        size_t model_size = infos[i + num_old_models].rows * infos[i + num_old_models].cols;
        for(size_t j = 0; j < model_size; j++){
            out_file << models[i]->get_data(j) << std::endl;
        }
    }
    out_file.close();

    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x278, %rsp            # imm = 0x278
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
testl %r8d, %r8d
movq %rcx, 0x8(%rsp)
je 0x15e58
leaq 0x10(%rsp), %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x15896
movq 0x18(%rsp), %r14
subq 0x10(%rsp), %r14
sarq $0x3, %r14
jmp 0x15e5b
xorl %r14d, %r14d
movq 0x8(%rbx), %r15
subq (%rbx), %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x160ea
leaq 0x60(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %r13, %rdi
callq 0x1694e
movb $0x1, %bpl
testb %al, %al
je 0x15ec7
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x160ea
leaq 0x48(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %r13, %rdi
callq 0x1694e
movl %eax, %ebp
xorb $0x1, %bpl
leaq 0x48(%rsp), %rdi
callq 0x164c4
leaq 0x60(%rsp), %rdi
callq 0x164c4
testb %bpl, %bpl
jne 0x16062
leaq 0x78(%rsp), %rdi
pushq $0x10
popq %rdx
movq %r12, %rsi
callq 0x9660
leaq 0x78(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x9320
movq %rax, %rdi
callq 0x9180
sarq $0x3, %r15
addq %r15, %r14
leaq 0x78(%rsp), %rdi
movq %r14, %rsi
callq 0x9210
movq %rax, %rdi
callq 0x9180
movq 0x30(%rsp), %r15
movq 0x38(%rsp), %r12
leaq 0x78(%rsp), %r14
cmpq %r12, %r15
je 0x15f73
movsbl (%r15), %esi
movq %r14, %rdi
callq 0x93c0
movq %rax, %rdi
callq 0x9180
movq 0x8(%r15), %rsi
movq %r14, %rdi
callq 0x9210
movq %rax, %rdi
callq 0x9180
movq 0x10(%r15), %rsi
movq %r14, %rdi
callq 0x9210
movq %rax, %rdi
callq 0x9180
addq $0x18, %r15
jmp 0x15f2c
movb %bpl, 0x7(%rsp)
movq 0x18(%rsp), %r12
subq 0x10(%rsp), %r12
sarq $0x3, %r12
xorl %r13d, %r13d
leaq 0x78(%rsp), %r14
cmpq %r12, %r13
je 0x15fde
movq 0x30(%rsp), %rax
imulq $0x18, %r13, %rcx
movq 0x10(%rax,%rcx), %rbp
imulq 0x8(%rax,%rcx), %rbp
xorl %r15d, %r15d
cmpq %r15, %rbp
je 0x15fd9
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %rdi
movq %r15, %rsi
callq 0x16a48
movss (%rax), %xmm0
movq %r14, %rdi
callq 0x9270
movq %rax, %rdi
callq 0x9180
incq %r15
jmp 0x15faa
incq %r13
jmp 0x15f8e
movq 0x8(%rbx), %rax
subq (%rbx), %rax
sarq $0x3, %rax
movq %rax, 0x8(%rsp)
xorl %ebp, %ebp
imulq $0x18, %r12, %r12
leaq 0x78(%rsp), %r14
cmpq 0x8(%rsp), %rbp
je 0x16049
imulq $0x18, %rbp, %rax
addq 0x30(%rsp), %rax
movq 0x10(%r12,%rax), %r13
imulq 0x8(%r12,%rax), %r13
xorl %r15d, %r15d
cmpq %r15, %r13
je 0x16044
movq (%rbx), %rax
movq (%rax,%rbp,8), %rdi
movq %r15, %rsi
callq 0x16a48
movss (%rax), %xmm0
movq %r14, %rdi
callq 0x9270
movq %rax, %rdi
callq 0x9180
incq %r15
jmp 0x16017
incq %rbp
jmp 0x15ff9
leaq 0x78(%rsp), %rdi
callq 0x94b0
leaq 0x78(%rsp), %rdi
callq 0x94f0
movb 0x7(%rsp), %bpl
xorb $0x1, %bpl
leaq 0x30(%rsp), %rdi
callq 0x16860
leaq 0x10(%rsp), %rdi
callq 0x164c4
movl %ebp, %eax
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x160c0
jmp 0x160a6
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x164c4
jmp 0x160ae
movq %rax, %rbx
jmp 0x160d7
movq %rax, %rbx
jmp 0x160cd
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x164c4
jmp 0x160cd
jmp 0x160c0
jmp 0x160c0
jmp 0x160c0
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x94f0
leaq 0x30(%rsp), %rdi
callq 0x16860
leaq 0x10(%rsp), %rdi
callq 0x164c4
movq %rbx, %rdi
callq 0x95e0
nop