// File: /workspace/github_cpp_makefile_star2_O1/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 $0x508, %rsp            # imm = 0x508
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 0x388(%rsp), %rdi
callq 0x295e8
leaq 0x208(%rsp), %rdi
leaq 0x388(%rsp), %rsi
movq %rdi, %r13
callq 0x2935c
movl %ebx, 0x38(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x388(%rsp), %rsi
callq 0x2e600
movl 0x568(%rsp), %r14d
movl 0x560(%rsp), %ebx
movl 0x558(%rsp), %ebp
leaq 0x10(%rsp), %r15
movq %rax, %rdi
movq %r15, %rsi
callq 0x5070
incq 0x10(%r15)
movl %ebp, %eax
andl $0x1, %eax
movl %ebx, %ecx
andl $0x1, %ecx
leal (%rcx,%rax,2), %eax
movl %r14d, %ecx
andl $0x3, %ecx
leal (%rcx,%rax,4), %eax
movq %rax, 0x60(%rsp)
leaq 0x270(%rsp), %r14
leaq 0xf0(%rsp), %r15
movl $0xd0, %r12d
movq %r13, %rax
addq %r12, %r13
leaq 0x88(%rsp), %rcx
addq %rcx, %r12
movl $0x150, %edx            # imm = 0x150
leaq (%rax,%rdx), %rsi
movq %rsi, 0x70(%rsp)
addq %rcx, %rdx
movq %rdx, 0x78(%rsp)
movl $0x168, %edx            # imm = 0x168
addq %rdx, %rax
movq %rax, 0x80(%rsp)
addq %rcx, %rdx
movq %rdx, 0x68(%rsp)
xorl %ebp, %ebp
cmpq $0x0, 0x20(%rsp)
je 0x1698a
cmpq $0x0, 0x20(%rsp)
je 0x16926
movq 0x10(%rsp), %rsi
addq $0x10, %rsi
leaq 0x88(%rsp), %rdi
callq 0x2935c
movq 0x10(%rsp), %rsi
leaq 0x10(%rsp), %rdi
callq 0x2e644
cmpl $0x752f, %ebp           # imm = 0x752F
jl 0x16736
movl $0x1, %r14d
subq $0x8, %rsp
movq 0x30(%rsp), %rdi
movl 0x14(%rsp), %edx
movq 0x68(%rsp), %rcx
movl 0x578(%rsp), %r8d
movl 0x558(%rsp), %r9d
pushq 0x38(%rsp)
callq 0x1b9aa
addq $0x10, %rsp
jmp 0x1687f
movl $0xffffffff, 0x3c(%rsp) # imm = 0xFFFFFFFF
subq $0x8, %rsp
leaq 0x90(%rsp), %rsi
movq 0x548(%rsp), %rdx
movq 0x60(%rsp), %rcx
movq 0x550(%rsp), %r8
movl 0x568(%rsp), %r9d
leaq 0x44(%rsp), %rax
pushq %rax
callq 0x174a2
addq $0x10, %rsp
incl %eax
cmpl $0x3, %eax
ja 0x1687c
leaq 0x1ab08(%rip), %rcx     # 0x31290
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x4, %r14d
jmp 0x1687f
leaq 0x40(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x2e600
movq %rax, %rdi
leaq 0x40(%rsp), %rsi
callq 0x5070
incq 0x50(%rsp)
jmp 0x1687c
movl 0x1e0(%rsp), %eax
cmpl 0x360(%rsp), %eax
jle 0x1687c
movl 0x88(%rsp), %eax
movl %eax, 0x208(%rsp)
movl $0x8, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x208, %rdi            # imm = 0x208
leaq (%rsp,%rbx), %rsi
addq $0x88, %rsi
callq 0xa67a
addq $0x18, %rbx
cmpq $0x68, %rbx
jne 0x167ec
xorl %ebx, %ebx
leaq (%r14,%rbx), %rdi
leaq (%r15,%rbx), %rsi
callq 0x2ab8a
addq $0x30, %rbx
cmpq $0x60, %rbx
jne 0x16813
movl 0x150(%rsp), %eax
movl %eax, 0x2d0(%rsp)
xorl %ebx, %ebx
leaq (%rbx,%r13), %rdi
leaq (%r12,%rbx), %rsi
callq 0x51c0
addq $0x20, %rbx
cmpq $0x80, %rbx
jne 0x1683a
movq 0x78(%rsp), %rcx
movl 0x10(%rcx), %eax
movq 0x70(%rsp), %rdx
movl %eax, 0x10(%rdx)
movups (%rcx), %xmm0
movups %xmm0, (%rdx)
movq 0x80(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x2aab4
xorl %r14d, %r14d
movq 0x1f0(%rsp), %rdi
testq %rdi, %rdi
je 0x1689c
movq 0x200(%rsp), %rsi
subq %rdi, %rsi
callq 0x5340
incl %ebp
movl $0x140, %ebx            # imm = 0x140
leaq (%rsp,%rbx), %rax
addq $0x88, %rax
movq -0x10(%rax), %rdi
cmpq %rdi, %rax
je 0x168c1
movq (%rax), %rsi
incq %rsi
callq 0x5340
addq $-0x20, %rbx
cmpq $0xc0, %rbx
jne 0x168a3
movl $0x98, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x88, %rdi
callq 0x2a5c6
addq $-0x30, %rbx
cmpq $0x38, %rbx
jne 0x168d3
movl $0x50, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x88, %rdi
callq 0xe7ca
addq $-0x18, %rbx
cmpq $-0x10, %rbx
jne 0x168f2
testb $0x3, %r14b
jne 0x16970
cmpq $0x0, 0x20(%rsp)
leaq 0x270(%rsp), %r14
jne 0x166cf
movq 0x40(%rsp), %rsi
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x2e67a
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0x2ae1a
movq %rbx, 0x48(%rsp)
movq %rbx, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movb $0x1, %bl
cmpq $0x0, 0x20(%rsp)
je 0x1697a
leaq 0x10(%rsp), %rsi
callq 0x1c8bc
jmp 0x1697a
xorl %ebx, %ebx
leaq 0x270(%rsp), %r14
testb %bl, %bl
jne 0x166b7
movb $0x1, %r14b
jmp 0x16a67
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x2ae1a
movq %rbx, 0x18(%rsp)
movq %rbx, 0x10(%rsp)
xorl %r14d, %r14d
movq %r14, 0x20(%rsp)
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0x2ae1a
movq %rbx, 0x48(%rsp)
movq %rbx, 0x40(%rsp)
movq %r14, 0x50(%rsp)
cmpl $0x0, 0x360(%rsp)
jle 0x16a67
movl 0x560(%rsp), %eax
movb $0x1, %r14b
testl %eax, %eax
je 0x16a25
subq $0x8, %rsp
leaq 0x210(%rsp), %rsi
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rdx
movq 0x60(%rsp), %rcx
movl 0x40(%rsp), %r8d
movl 0x14(%rsp), %r9d
movl 0x578(%rsp), %eax
pushq %rax
pushq 0x70(%rsp)
movl 0x568(%rsp), %eax
pushq %rax
callq 0x1e236
addq $0x20, %rsp
jmp 0x16a67
subq $0x8, %rsp
leaq 0x210(%rsp), %rsi
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rdx
movq 0x60(%rsp), %rcx
movl 0x40(%rsp), %r8d
movl 0x14(%rsp), %r9d
movl 0x578(%rsp), %eax
pushq %rax
pushq 0x70(%rsp)
movl 0x568(%rsp), %eax
pushq %rax
callq 0x1cb66
addq $0x20, %rsp
movq 0x370(%rsp), %rdi
testq %rdi, %rdi
je 0x16a84
movq 0x380(%rsp), %rsi
subq %rdi, %rsi
callq 0x5340
movl $0x140, %ebx            # imm = 0x140
leaq (%rsp,%rbx), %rax
addq $0x208, %rax            # imm = 0x208
movq -0x10(%rax), %rdi
cmpq %rdi, %rax
je 0x16aa7
movq (%rax), %rsi
incq %rsi
callq 0x5340
addq $-0x20, %rbx
cmpq $0xc0, %rbx
jne 0x16a89
movl $0x98, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x208, %rdi            # imm = 0x208
callq 0x2a5c6
addq $-0x30, %rbx
cmpq $0x38, %rbx
jne 0x16ab9
movl $0x50, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x208, %rdi            # imm = 0x208
callq 0xe7ca
addq $-0x18, %rbx
cmpq $-0x10, %rbx
jne 0x16ad8
movq 0x4f0(%rsp), %rdi
testq %rdi, %rdi
je 0x16b0f
movq 0x500(%rsp), %rsi
subq %rdi, %rsi
callq 0x5340
movl $0x140, %ebx            # imm = 0x140
leaq (%rsp,%rbx), %rax
addq $0x388, %rax            # imm = 0x388
movq -0x10(%rax), %rdi
cmpq %rdi, %rax
je 0x16b32
movq (%rax), %rsi
incq %rsi
callq 0x5340
addq $-0x20, %rbx
cmpq $0xc0, %rbx
jne 0x16b14
movl $0x98, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x388, %rdi            # imm = 0x388
callq 0x2a5c6
addq $-0x30, %rbx
cmpq $0x38, %rbx
jne 0x16b44
movl $0x50, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x388, %rdi            # imm = 0x388
callq 0xe7ca
addq $-0x18, %rbx
cmpq $-0x10, %rbx
jne 0x16b63
leaq 0x40(%rsp), %rdi
callq 0x2ae1a
leaq 0x10(%rsp), %rdi
callq 0x2ae1a
movl %r14d, %eax
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x3c(%rsp), %eax
movl 0x560(%rsp), %ecx
testl %ecx, %ecx
jne 0x16bec
subq $0x8, %rsp
leaq 0x90(%rsp), %rsi
leaq 0x18(%rsp), %rcx
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rdx
movl 0x560(%rsp), %r9d
pushq %rax
movl 0x578(%rsp), %eax
pushq %rax
pushq $0x0
callq 0x18f1c
addq $0x20, %rsp
subq $0x8, %rsp
leaq 0x90(%rsp), %rsi
leaq 0x18(%rsp), %rcx
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rdx
movq 0x60(%rsp), %r8
movl 0x560(%rsp), %r9d
pushq %rax
movl 0x578(%rsp), %eax
pushq %rax
movl 0x578(%rsp), %eax
pushq %rax
callq 0x1969c
addq $0x20, %rsp
jmp 0x16c44
jmp 0x16c44
movq %rax, %rbx
jmp 0x16c6a
movq %rax, %rbx
jmp 0x16c77
jmp 0x16c44
jmp 0x16c4d
jmp 0x16c4d
jmp 0x16c4d
movq %rax, %rbx
jmp 0x16c5d
jmp 0x16c4d
jmp 0x16c4d
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x29560
leaq 0x208(%rsp), %rdi
callq 0x29560
leaq 0x388(%rsp), %rdi
callq 0x29560
leaq 0x40(%rsp), %rdi
callq 0x2ae1a
leaq 0x10(%rsp), %rdi
callq 0x2ae1a
movq %rbx, %rdi
callq 0x55c0
nop