// File: /workspace/github_cpp_makefile_star2_O3/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 $0x518, %rsp            # imm = 0x518
movl %r9d, %ebx
movl %edx, 0xc(%rsp)
movq %rdi, 0x28(%rsp)
leaq 0x10(%rsp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
xorl %edx, %edx
movq %rdx, 0x10(%rax)
leaq 0x40(%rsp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
movq %rdx, 0x10(%rax)
movq %r8, 0x58(%rsp)
movl 0x8(%r8), %esi
movq 0x8(%rcx), %rax
movq %rcx, 0x30(%rsp)
subq (%rcx), %rax
shrq $0x3, %rax
imull $0xba2e8ba3, %eax, %edx # imm = 0xBA2E8BA3
leaq 0x398(%rsp), %rdi
callq 0x2915c
leaq 0x218(%rsp), %rdi
leaq 0x398(%rsp), %rsi
movq %rdi, %r15
callq 0x28ef6
movl %ebx, 0x38(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x398(%rsp), %rsi
callq 0x2e160
movl 0x578(%rsp), %r14d
movl 0x570(%rsp), %ebx
movl 0x568(%rsp), %ebp
leaq 0x10(%rsp), %r12
movq %rax, %rdi
movq %r12, %rsi
callq 0x5070
movl %ebp, %eax
andl $0x1, %eax
movl %ebx, %ecx
andl $0x1, %ecx
movl %r14d, %edx
andl $0x3, %edx
incq 0x10(%r12)
leal (%rcx,%rax,2), %eax
leal (%rdx,%rax,4), %eax
movq %rax, 0x60(%rsp)
je 0x167e8
movl $0xd0, %r14d
leaq (%r15,%r14), %rbx
leaq 0x98(%rsp), %rcx
addq %rcx, %r14
movl $0x150, %eax            # imm = 0x150
leaq (%r15,%rax), %rdx
movq %rdx, 0x78(%rsp)
addq %rcx, %rax
movq %rax, 0x80(%rsp)
movl $0x168, %eax            # imm = 0x168
leaq (%r15,%rax), %rdx
movq %rdx, 0x68(%rsp)
addq %rcx, %rax
movq %rax, 0x70(%rsp)
movl $0x98, %eax
addq %rax, %r15
movq %r15, 0x90(%rsp)
addq %rax, %rcx
movq %rcx, 0x88(%rsp)
xorl %ebp, %ebp
leaq 0x98(%rsp), %r15
movq 0x10(%rsp), %rsi
addq $0x10, %rsi
movq %r15, %rdi
callq 0x28ef6
movq 0x10(%rsp), %rsi
leaq 0x10(%rsp), %rdi
callq 0x2e1a4
cmpl $0x752f, %ebp           # imm = 0x752F
jb 0x1659e
movl $0x1, %r13d
subq $0x8, %rsp
movq 0x30(%rsp), %rdi
movl 0x14(%rsp), %edx
movq 0x68(%rsp), %rcx
movl 0x588(%rsp), %r8d
movl 0x568(%rsp), %r9d
pushq 0x38(%rsp)
callq 0x1b620
addq $0x10, %rsp
jmp 0x166f0
movl $0xffffffff, 0x3c(%rsp) # imm = 0xFFFFFFFF
subq $0x8, %rsp
movq %r15, %rsi
movq 0x558(%rsp), %rdx
movq 0x60(%rsp), %rcx
movq 0x560(%rsp), %r8
movl 0x578(%rsp), %r9d
leaq 0x44(%rsp), %rax
pushq %rax
callq 0x172ac
addq $0x10, %rsp
incl %eax
cmpl $0x3, %eax
ja 0x166ed
leaq 0x1acd5(%rip), %rcx     # 0x312c0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x4, %r13d
jmp 0x166f0
leaq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0x2e160
movq %rax, %rdi
leaq 0x40(%rsp), %rsi
callq 0x5070
incq 0x50(%rsp)
jmp 0x166ed
movl 0x1f0(%rsp), %eax
cmpl 0x370(%rsp), %eax
jle 0x166ed
movl 0x98(%rsp), %eax
movl %eax, 0x218(%rsp)
movl $0x8, %r12d
leaq (%rsp,%r12), %rdi
addq $0x218, %rdi            # imm = 0x218
leaq (%rsp,%r12), %rsi
addq $0x98, %rsi
callq 0xa5da
addq $0x18, %r12
cmpq $0x68, %r12
jne 0x1664b
leaq 0x280(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x2a6b0
movq 0x90(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x2a6b0
movl 0x160(%rsp), %eax
movl %eax, 0x2e0(%rsp)
xorl %r12d, %r12d
leaq (%rbx,%r12), %rdi
leaq (%r14,%r12), %rsi
callq 0x51c0
addq $0x20, %r12
cmpq $0x80, %r12
jne 0x166ab
movq 0x80(%rsp), %rcx
movl 0x10(%rcx), %eax
movq 0x78(%rsp), %rdx
movl %eax, 0x10(%rdx)
movups (%rcx), %xmm0
movups %xmm0, (%rdx)
movq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x2a5cc
xorl %r13d, %r13d
movq 0x200(%rsp), %rdi
testq %rdi, %rdi
je 0x1670d
movq 0x210(%rsp), %rsi
subq %rdi, %rsi
callq 0x5340
incl %ebp
movl $0x140, %r12d           # imm = 0x140
leaq (%rsp,%r12), %rax
addq $0x98, %rax
movq -0x10(%rax), %rdi
cmpq %rdi, %rax
je 0x16733
movq (%rax), %rsi
incq %rsi
callq 0x5340
addq $-0x20, %r12
cmpq $0xc0, %r12
jne 0x16715
movl $0x98, %r12d
leaq (%rsp,%r12), %rdi
addq $0x98, %rdi
callq 0x2a10c
addq $-0x30, %r12
cmpq $0x38, %r12
jne 0x16746
movl $0x50, %r12d
leaq (%rsp,%r12), %rdi
addq $0x98, %rdi
callq 0xe71c
addq $-0x18, %r12
cmpq $-0x10, %r12
jne 0x16766
testb $0x3, %r13b
jne 0x16883
cmpq $0x0, 0x20(%rsp)
jne 0x1653c
movq 0x40(%rsp), %rsi
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x2e1da
leaq 0x40(%rsp), %r12
movq %r12, %rdi
callq 0x2a92a
movq %r12, 0x48(%rsp)
movq %r12, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
cmpq $0x0, 0x20(%rsp)
je 0x167dc
leaq 0x10(%rsp), %rsi
callq 0x1c49c
cmpq $0x0, 0x20(%rsp)
jne 0x1653c
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x2a92a
movq %rbx, 0x18(%rsp)
movq %rbx, 0x10(%rsp)
xorl %r15d, %r15d
movq %r15, 0x20(%rsp)
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0x2a92a
movq %rbx, 0x48(%rsp)
movq %rbx, 0x40(%rsp)
movq %r15, 0x50(%rsp)
cmpl $0x0, 0x370(%rsp)
jle 0x168ca
movl 0x570(%rsp), %eax
movb $0x1, %r15b
testl %eax, %eax
je 0x16888
subq $0x8, %rsp
leaq 0x220(%rsp), %rsi
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rdx
movq 0x60(%rsp), %rcx
movl 0x40(%rsp), %r8d
movl 0x14(%rsp), %r9d
movl 0x588(%rsp), %eax
pushq %rax
pushq 0x70(%rsp)
movl 0x578(%rsp), %eax
pushq %rax
callq 0x1de68
addq $0x20, %rsp
jmp 0x168ca
movb $0x1, %r15b
jmp 0x168ca
subq $0x8, %rsp
leaq 0x220(%rsp), %rsi
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rdx
movq 0x60(%rsp), %rcx
movl 0x40(%rsp), %r8d
movl 0x14(%rsp), %r9d
movl 0x588(%rsp), %eax
pushq %rax
pushq 0x70(%rsp)
movl 0x578(%rsp), %eax
pushq %rax
callq 0x1c74a
addq $0x20, %rsp
movq 0x380(%rsp), %rdi
testq %rdi, %rdi
je 0x168e7
movq 0x390(%rsp), %rsi
subq %rdi, %rsi
callq 0x5340
movl $0x140, %ebx            # imm = 0x140
leaq (%rsp,%rbx), %rax
addq $0x218, %rax            # imm = 0x218
movq -0x10(%rax), %rdi
cmpq %rdi, %rax
je 0x1690a
movq (%rax), %rsi
incq %rsi
callq 0x5340
addq $-0x20, %rbx
cmpq $0xc0, %rbx
jne 0x168ec
movl $0x98, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x218, %rdi            # imm = 0x218
callq 0x2a10c
addq $-0x30, %rbx
cmpq $0x38, %rbx
jne 0x1691c
movl $0x50, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x218, %rdi            # imm = 0x218
callq 0xe71c
addq $-0x18, %rbx
cmpq $-0x10, %rbx
jne 0x1693b
movq 0x500(%rsp), %rdi
testq %rdi, %rdi
je 0x16972
movq 0x510(%rsp), %rsi
subq %rdi, %rsi
callq 0x5340
movl $0x140, %ebx            # imm = 0x140
leaq (%rsp,%rbx), %rax
addq $0x398, %rax            # imm = 0x398
movq -0x10(%rax), %rdi
cmpq %rdi, %rax
je 0x16995
movq (%rax), %rsi
incq %rsi
callq 0x5340
addq $-0x20, %rbx
cmpq $0xc0, %rbx
jne 0x16977
movl $0x98, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x398, %rdi            # imm = 0x398
callq 0x2a10c
addq $-0x30, %rbx
cmpq $0x38, %rbx
jne 0x169a7
movl $0x50, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x398, %rdi            # imm = 0x398
callq 0xe71c
addq $-0x18, %rbx
cmpq $-0x10, %rbx
jne 0x169c6
leaq 0x40(%rsp), %rdi
callq 0x2a92a
leaq 0x10(%rsp), %rdi
callq 0x2a92a
movl %r15d, %eax
addq $0x518, %rsp            # imm = 0x518
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x3c(%rsp), %eax
movl 0x570(%rsp), %ecx
testl %ecx, %ecx
jne 0x16a4f
subq $0x8, %rsp
leaq 0xa0(%rsp), %rsi
leaq 0x18(%rsp), %rcx
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rdx
movl 0x570(%rsp), %r9d
pushq %rax
movl 0x588(%rsp), %eax
pushq %rax
pushq $0x0
callq 0x18c74
addq $0x20, %rsp
subq $0x8, %rsp
leaq 0xa0(%rsp), %rsi
leaq 0x18(%rsp), %rcx
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rdx
movq 0x60(%rsp), %r8
movl 0x570(%rsp), %r9d
pushq %rax
movl 0x588(%rsp), %eax
pushq %rax
movl 0x588(%rsp), %eax
pushq %rax
callq 0x19372
addq $0x20, %rsp
jmp 0x16aa9
jmp 0x16aa9
movq %rax, %rbx
jmp 0x16acd
movq %rax, %rbx
jmp 0x16ada
jmp 0x16ab0
jmp 0x16ab0
jmp 0x16ab0
jmp 0x16aa9
jmp 0x16ab0
movq %rax, %rbx
jmp 0x16ac0
jmp 0x16ab0
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x290d4
leaq 0x218(%rsp), %rdi
callq 0x290d4
leaq 0x398(%rsp), %rdi
callq 0x290d4
leaq 0x40(%rsp), %rdi
callq 0x2a92a
leaq 0x10(%rsp), %rdi
callq 0x2a92a
movq %rbx, %rdi
callq 0x55c0