// File: /workspace/github_cpp_makefile_star2_O2/medvedevgroup[P]varmatch/src/wholegenome.cpp
// Function: WholeGenome::MatchingSingleClusterBaseExtending(int, int, std::vector<DiploidVariant, std::allocator<DiploidVariant>>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, int, std::multimap<int, int, std::less<int>, std::allocator<std::pair<int const, int>>>**, std::vector<int, std::allocator<int>>&, int, int, int, int, int)
bool WholeGenome::MatchingSingleClusterBaseExtending(int cluster_index,
                                                    int thread_index,
                                                    vector<DiploidVariant> & variant_list,
                                                    string & subsequence,
                                                    int offset,
                                                    multimap<int, int> * choices_by_pos[],
                                                    vector<int> & sync_points,
                                                    int chr_id,
                                                    int score_unit,
                                                    int match_mode,
                                                    int score_scheme,
                                                    int threshold_index){
    //--------------for unit test------------------------------
    //dout << variant_list.size() << endl;

    //int chr_id = 0;
    //-------------end unit test-------------------------------

    //cout << chr_id << "," << cluster_index << "," << variant_list.size() << endl;

    // so a legal sync_points vector contains at least two
    // first is the end of variant, there should be at least one variant
    // second is the end of subsequence, there should be at least one nt not influenced by a variant

    list<SequencePath> current_path_list;
    list<SequencePath> next_path_list;
    SequencePath sp(subsequence.length(), variant_list.size());
    SequencePath best_path = sp;
    current_path_list.push_back(sp);
    int considered_path_num = 0;
    int mode_index = GetIndexFromMatchScore(score_unit, match_mode, score_scheme);
    while(current_path_list.size() != 0){
        bool reach_sync_point = true;
        // extend path before reaches sync points
        //cout << "\t" << current_path_list.size() << "," << considered_path_num << endl;
        
        while(current_path_list.size() != 0){
            SequencePath path = current_path_list.front();
            current_path_list.pop_front();
            considered_path_num ++;
            if (considered_path_num >= MAX_SEARCH_SPACE){
                //cout << "direct matching large variant cluster" << endl;
                //cout << variant_list.size() << "," << considered_path_num << endl;
                return MatchingSingleClusterDirectly(cluster_index, 
                    thread_index, 
                    mode_index, 
                    threshold_index, 
                    chr_id,
                    variant_list);
            }
            //dout << path.current_genome_pos << ":" << current_path_list.size() << endl;
            //PrintPath(path);
            int variant_need_decision = -1;
            int is_extend = PathExtendOneStep(path, choices_by_pos, subsequence, sync_points, match_mode, variant_need_decision);
            //cout << variant_need_decision << endl;
            //PrintPath(path);
            if(is_extend == -1){
                // discard path
                continue;
            }
            else if(is_extend == 0){
                next_path_list.push_back(path);
                // here the path is supposed to reach the next sync point
                // so it goes into next path list, and decrease the number of current path list
            }else if(is_extend == 1){
                if(match_mode == 0){
                    // PathMakeDecision(path,
                    //                  variant_list,
                    //                  choices_by_pos,
                    //                  current_path_list,
                    //                  subsequence,
                    //                  score_unit,
                    //                  match_mode,
                    //                  score_scheme);

                    VariantMakeDecision(path,
                                         variant_list,
                                         current_path_list,
                                         subsequence,
                                         score_unit,
                                         match_mode,
                                         score_scheme,
                                         variant_need_decision);
                }else{
                    // PathMakeDecisionNoGenotype(path,
                    //                            variant_list,
                    //                            choices_by_pos,
                    //                            current_path_list,
                    //                            subsequence,
                    //                            score_unit,
                    //                            match_mode,
                    //                            score_scheme);

                    VariantMakeDecisionNoGenotype(path,
                                                 variant_list,
                                                 current_path_list,
                                                 subsequence,
                                                 score_unit,
                                                 match_mode,
                                                 score_scheme,
                                                 variant_need_decision);
                }
            }else if(is_extend == 2){
                if(path.score > best_path.score){
                    best_path = path; // only when you reach the very end can you be considered as best path
                    //PrintPath(best_path);
                }
            }
        }
        current_path_list = next_path_list;
        next_path_list.clear();
        if(current_path_list.size() > 0){
            //int current_genome_pos = current_path_list.front().current_genome_pos;
            // after revise, we do not need this check
            //if(sync_points.find(current_genome_pos) != sync_points.end()){
                //dout << "converge paths at position: " << current_genome_pos << endl;
                //dout << "before converge: " << current_path_list.size() << endl;
                ConvergePaths(current_path_list);
                //dout << "after converge: " << current_path_list.size() << endl;
            //}
        }
    }
    current_path_list.clear();
    next_path_list.clear();
    // print best_path
    if(best_path.score <= 0) return false;
    // if(considered_path_num > max_path_num_by_mode_by_thread[thread_index][mode_index]){
    //     max_path_num_by_mode_by_thread[thread_index][mode_index] = considered_path_num;
    // }
    //dout << "new method: " << best_path.score << endl;

    //PrintPath(best_path);

    //==========================output ======================

    //return true;
    if(match_mode == 0){
        ConstructMatchRecord(best_path,
                             variant_list,
                             subsequence,
                             offset,
                             thread_index,
                             chr_id,
                             mode_index,
                             threshold_index);
    }else{
        ConstructMatchRecordNoGenotype(best_path,
                                       variant_list,
                                       subsequence,
                                       offset,
                                       thread_index,
                                       chr_id,
                                       mode_index,
                                       threshold_index);
    }
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4d8, %rsp            # imm = 0x4D8
movl %r9d, %r15d
movq %r8, %r14
movq %rcx, %rbx
movl %edx, %ebp
leaq 0x28(%rsp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
andq $0x0, 0x10(%rax)
movq %rdi, 0x8(%rsp)
leaq 0x40(%rsp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
andq $0x0, 0x10(%rax)
movl 0x8(%r8), %esi
movq 0x8(%rcx), %rax
subq (%rcx), %rax
pushq $0x58
popq %rcx
cqto
idivq %rcx
leaq 0x358(%rsp), %rdi
movl %eax, %edx
callq 0x1a618
leaq 0x58(%rsp), %rdi
leaq 0x358(%rsp), %rsi
callq 0x1a3e8
movl %r15d, 0x18(%rsp)
movl %ebp, 0x4(%rsp)
movq %rbx, 0x10(%rsp)
leaq 0x28(%rsp), %rdi
leaq 0x358(%rsp), %rsi
callq 0x1a5a4
movl 0x538(%rsp), %edx
movl 0x530(%rsp), %ecx
movl 0x528(%rsp), %eax
movq 0x510(%rsp), %r12
andl $0x1, %eax
andl $0x1, %ecx
leal (%rcx,%rax,2), %eax
movl %edx, %ecx
andl $0x3, %ecx
leal (%rcx,%rax,4), %eax
movq %rax, 0x20(%rsp)
xorl %r13d, %r13d
leaq 0x28(%rsp), %r15
leaq 0x1d8(%rsp), %rbx
leaq 0x1c(%rsp), %rbp
cmpq $0x0, 0x38(%rsp)
je 0x107af
pushq $0x1
popq %rax
testq %rax, %rax
je 0x106ff
movq 0x28(%rsp), %rsi
addq $0x10, %rsi
movq %rbx, %rdi
callq 0x1a3e8
movq %r15, %rdi
callq 0x1a7e2
cmpl $0x752f, %r13d          # imm = 0x752F
jge 0x10734
orl $-0x1, 0x1c(%rsp)
subq $0x8, %rsp
movq %rbx, %rsi
movq %r12, %rdx
movq %r14, %rcx
movq 0x520(%rsp), %r8
movl 0x538(%rsp), %r9d
pushq %rbp
callq 0x10ea6
popq %rcx
popq %rdx
testl %eax, %eax
je 0x106dd
cmpl $0x2, %eax
je 0x106be
cmpl $0x1, %eax
jne 0x106ea
jmp 0x10865
movl 0x330(%rsp), %eax
cmpl 0x1b0(%rsp), %eax
jle 0x106ea
leaq 0x58(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a7ea
jmp 0x106ea
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a5a4
movq %rbx, %rdi
callq 0x1a5b0
movq 0x38(%rsp), %rax
incl %r13d
jmp 0x10652
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
callq 0x1a89e
leaq 0x40(%rsp), %rdi
callq 0x1a8ba
cmpq $0x0, 0x38(%rsp)
je 0x10643
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x140a2
jmp 0x10643
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %edx
movq 0x28(%rsp), %rcx
movl 0x548(%rsp), %r8d
movl 0x528(%rsp), %r9d
pushq 0x18(%rsp)
callq 0x13a1a
popq %rcx
popq %rdx
leaq 0x1d8(%rsp), %rdi
callq 0x1a5b0
movb $0x1, %bl
leaq 0x58(%rsp), %rdi
callq 0x1a5b0
leaq 0x358(%rsp), %rdi
callq 0x1a5b0
leaq 0x40(%rsp), %rdi
callq 0x1bfcc
leaq 0x28(%rsp), %rdi
callq 0x1bfcc
movl %ebx, %eax
addq $0x4d8, %rsp            # imm = 0x4D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x28(%rsp), %rdi
callq 0x1a8ba
leaq 0x40(%rsp), %rdi
callq 0x1a8ba
cmpl $0x0, 0x1b0(%rsp)
jle 0x1081c
movl 0x530(%rsp), %eax
movb $0x1, %bl
testl %eax, %eax
je 0x10823
subq $0x8, %rsp
leaq 0x60(%rsp), %rsi
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
movq %r14, %rcx
movl 0x20(%rsp), %r8d
movl 0xc(%rsp), %r9d
movl 0x548(%rsp), %eax
pushq %rax
pushq 0x30(%rsp)
movl 0x538(%rsp), %eax
pushq %rax
callq 0x14c22
addq $0x20, %rsp
jmp 0x10770
xorl %ebx, %ebx
jmp 0x10770
subq $0x8, %rsp
leaq 0x60(%rsp), %rsi
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
movq %r14, %rcx
movl 0x20(%rsp), %r8d
movl 0xc(%rsp), %r9d
movl 0x548(%rsp), %eax
pushq %rax
pushq 0x30(%rsp)
movl 0x538(%rsp), %eax
pushq %rax
callq 0x141aa
addq $0x20, %rsp
jmp 0x10770
movl 0x1c(%rsp), %eax
movl 0x530(%rsp), %ecx
testl %ecx, %ecx
jne 0x108ab
subq $0x8, %rsp
leaq 0x1e0(%rsp), %rsi
leaq 0x30(%rsp), %rcx
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
movl 0x530(%rsp), %r9d
pushq %rax
movl 0x548(%rsp), %eax
pushq %rax
pushq $0x0
callq 0x11f08
addq $0x20, %rsp
subq $0x8, %rsp
leaq 0x1e0(%rsp), %rsi
leaq 0x30(%rsp), %rcx
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
movq %r14, %r8
movl 0x530(%rsp), %r9d
pushq %rax
movl 0x548(%rsp), %eax
pushq %rax
movl 0x548(%rsp), %eax
pushq %rax
callq 0x12480
addq $0x20, %rsp
jmp 0x108ff
jmp 0x10904
jmp 0x108ff
movq %rax, %rbx
jmp 0x1091e
movq %rax, %rbx
jmp 0x1092b
jmp 0x108ff
jmp 0x10904
movq %rax, %rbx
jmp 0x10914
movq %rax, %rbx
leaq 0x1d8(%rsp), %rdi
callq 0x1a5b0
leaq 0x58(%rsp), %rdi
callq 0x1a5b0
leaq 0x358(%rsp), %rdi
callq 0x1a5b0
leaq 0x40(%rsp), %rdi
callq 0x1bfcc
leaq 0x28(%rsp), %rdi
callq 0x1bfcc
movq %rbx, %rdi
callq 0x5540
nop