// File: /workspace/github_cpp_makefile_star2_O2/medvedevgroup[P]varmatch/src/wholegenome.cpp
// Function: WholeGenome::MatchingSingleClusterDirectly(int, int, int, int, int, std::vector<DiploidVariant, std::allocator<DiploidVariant>>&)
bool WholeGenome::MatchingSingleClusterDirectly(int cluster_index, 
    int thread_index, 
    int mode_index, 
    int threshold_index, 
    int chr_id,
    vector<DiploidVariant> & variant_list){
    //cout << "direct match : " << variant_list.size() << endl;
    map<int, vector<DiploidVariant>> separate_var_list_map[2];
    for (int i = 0; i < variant_list.size(); i++) {
        int flag = 0;
        if (variant_list[i].flag) flag = 1; // flag indicate if the variant is from ref set(0) or query set(1)
        int pos = variant_list[i].pos;

        if(separate_var_list_map[flag].find(pos) == separate_var_list_map[flag].end()){
            separate_var_list_map[flag][pos] = vector<DiploidVariant>({variant_list[i]});
        }else{
            separate_var_list_map[flag][pos].push_back(variant_list[i]);
        }
    }

    //int match_mode = GetMatchmodeFromModeIndex(mode_index);

    for(auto baseline_it = separate_var_list_map[0].begin(); baseline_it != separate_var_list_map[0].end(); ++baseline_it){
        int pos = baseline_it->first;
        if(separate_var_list_map[1].find(pos) != separate_var_list_map[1].end()){
            vector<DiploidVariant> baseline_var_list = baseline_it->second;
            vector<DiploidVariant> query_var_list = separate_var_list_map[1][pos];
            for(auto baseline_var : baseline_var_list){
                for(auto query_var : query_var_list){
                    if(baseline_var == query_var){
                        DiploidVariant tv = baseline_var;
                        string match_record = chrname_by_chrid[chr_id] + "\t" + to_string(tv.pos+1) + "\t" + tv.ref + "\t" + tv.alts[0];
                        if(tv.multi_alts) match_record += "/" + tv.alts[1];
                        match_record += "\t.\t.\t.\t.\t.\n";
                        int edit_distance = CalculateEditDistance(tv, 0, 0);
                        match_records_by_mode_by_thread[thread_index][mode_index]->push_back(match_record);
                        baseline_total_match_num[thread_index][threshold_index]->at(mode_index)++;
                        query_total_match_num[thread_index][threshold_index]->at(mode_index)++;

                        baseline_total_edit_distance[thread_index][threshold_index]->at(mode_index) += edit_distance;
                        query_total_edit_distance[thread_index][threshold_index]->at(mode_index) += edit_distance;
                        break;
                    }
                }
            }
        }
    } 
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2c8, %rsp            # imm = 0x2C8
movl %r8d, 0x18(%rsp)
movl %ecx, (%rsp)
movl %edx, 0x10(%rsp)
movq %rdi, 0x30(%rsp)
movq 0x300(%rsp), %rbx
movl %r9d, 0x24(%rsp)
leaq 0xa8(%rsp), %rdx
xorl %eax, %eax
leaq (%rdx,%rax), %rcx
andl $0x0, 0xa8(%rsp,%rax)
andq $0x0, 0xb0(%rsp,%rax)
movq %rcx, 0xb8(%rsp,%rax)
movq %rcx, 0xc0(%rsp,%rax)
andq $0x0, 0xc8(%rsp,%rax)
addq $0x30, %rax
cmpq $0x60, %rax
jne 0x13a53
xorl %r13d, %r13d
leaq 0x270(%rsp), %r15
xorl %r14d, %r14d
movq (%rbx), %rcx
movq 0x8(%rbx), %rax
subq %rcx, %rax
cqto
pushq $0x58
popq %rsi
idivq %rsi
cmpq %r14, %rax
jbe 0x13b8e
movq %r15, %r12
movb 0x4c(%rcx,%r13), %r15b
movl (%rcx,%r13), %eax
movl %eax, 0x100(%rsp)
leaq 0xd0(%rsp), %rbp
testb %r15b, %r15b
jne 0x13adc
leaq 0xa0(%rsp), %rbp
movq %rbp, %rdi
leaq 0x100(%rsp), %rsi
callq 0x1fa1e
leaq 0xd8(%rsp), %rcx
testb %r15b, %r15b
jne 0x13b01
leaq 0xa8(%rsp), %rcx
cmpq %rcx, %rax
movq %r12, %r15
je 0x13b29
movq %rbp, %rdi
leaq 0x100(%rsp), %rsi
callq 0x1a90c
movq (%rbx), %rsi
addq %r13, %rsi
movq %rax, %rdi
callq 0x19bea
jmp 0x13b82
movq (%rbx), %rsi
addq %r13, %rsi
movq %r15, %rdi
callq 0x1a174
leaq 0x218(%rsp), %rdi
movq %r15, %rsi
pushq $0x1
popq %rdx
leaq 0x50(%rsp), %rcx
callq 0x1a8d2
movq %rbp, %rdi
leaq 0x100(%rsp), %rsi
callq 0x1a90c
movq %rax, %rdi
leaq 0x218(%rsp), %r12
movq %r12, %rsi
callq 0x1fdaa
movq %r12, %rdi
callq 0x19a38
movq %r15, %rdi
callq 0x19c16
incq %r14
addq $0x58, %r13
jmp 0x13a99
leaq 0xa8(%rsp), %rax
movq 0x10(%rax), %rcx
movq 0x30(%rsp), %rax
addq $0xb8, %rax
movq %rax, 0x48(%rsp)
movslq 0x10(%rsp), %rdx
movslq (%rsp), %rsi
movslq 0x18(%rsp), %rax
movq %rax, (%rsp)
leaq 0x270(%rsp), %r14
leaq 0x218(%rsp), %r12
leaq 0x100(%rsp), %r15
movq %rdx, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movq 0x40(%rsp), %rbp
leaq 0xa8(%rsp), %rax
cmpq %rax, %rcx
je 0x13f29
movq %rcx, 0x28(%rsp)
movl 0x20(%rcx), %eax
movl %eax, 0xc(%rsp)
leaq 0xd0(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x1fa1e
leaq 0xd8(%rsp), %rcx
cmpq %rcx, %rax
je 0x13f17
movq 0x28(%rsp), %rax
leaq 0x28(%rax), %rsi
leaq 0x88(%rsp), %rdi
callq 0x1a95a
leaq 0xd0(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x1a90c
leaq 0x70(%rsp), %rdi
movq %rax, %rsi
callq 0x1a95a
movq 0x88(%rsp), %rsi
movq 0x90(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq 0x10(%rsp), %rsi
je 0x13f00
movq %r14, %rdi
movq %rsi, 0x18(%rsp)
callq 0x1a174
movq 0x70(%rsp), %r13
movq 0x78(%rsp), %rbx
cmpq %rbx, %r13
je 0x13eea
movq %r12, %rdi
movq %r13, %rsi
callq 0x1a174
movq %r14, %rdi
movq %r12, %rsi
callq 0x1a05e
testb %al, %al
jne 0x13cc0
movq %r12, %rdi
callq 0x19c16
addq $0x58, %r13
jmp 0x13c8f
movq %r15, %rdi
movq %r14, %rsi
callq 0x1a174
movq 0x48(%rsp), %rdi
leaq 0x24(%rsp), %rsi
callq 0x19a5a
leaq 0x158(%rsp), %rdi
movq %rax, %rsi
leaq 0xe8bd(%rip), %rdx      # 0x225a9
callq 0x96b0
movl 0x100(%rsp), %esi
incl %esi
leaq 0x178(%rsp), %rbx
movq %rbx, %rdi
callq 0x8011
leaq 0x198(%rsp), %r13
movq %r13, %rdi
leaq 0x158(%rsp), %rsi
movq %rbx, %rdx
callq 0x94bb
leaq 0x1b8(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
leaq 0xe86f(%rip), %rdx      # 0x225a9
callq 0x7fce
leaq 0x1d8(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x108(%rsp), %rdx
callq 0x9691
leaq 0x1f8(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
leaq 0xe83a(%rip), %rdx      # 0x225a9
callq 0x7fce
movq 0x128(%rsp), %rdx
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x9691
movq %rbx, %rdi
callq 0x55e8
leaq 0x1d8(%rsp), %rdi
callq 0x55e8
leaq 0x1b8(%rsp), %rdi
callq 0x55e8
leaq 0x198(%rsp), %rdi
callq 0x55e8
leaq 0x178(%rsp), %rdi
callq 0x55e8
leaq 0x158(%rsp), %rdi
callq 0x55e8
cmpb $0x1, 0x141(%rsp)
jne 0x13e0c
movq 0x128(%rsp), %rdx
addq $0x20, %rdx
movq %rbx, %rdi
leaq 0xe7b9(%rip), %rsi      # 0x225ab
callq 0x9dae
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x55c0
movq %rbx, %rdi
callq 0x55e8
leaq 0x50(%rsp), %rdi
leaq 0xe795(%rip), %rsi      # 0x225ad
callq 0x55a0
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x104ac
movq 0x38(%rsp), %r15
movl %eax, %r13d
movq 0x178(%rbx), %rax
movq (%rax,%rbp,8), %rax
movq (%rax,%r15,8), %rdi
leaq 0x50(%rsp), %rsi
callq 0xc896
movq 0x180(%rbx), %rax
movq (%rax,%rbp,8), %rax
movq (%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq %r15, %rsi
callq 0x1a1ca
incl (%rax)
movq 0x188(%rbx), %rax
movq (%rax,%rbp,8), %rax
movq (%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq %r15, %rsi
callq 0x1a1ca
incl (%rax)
movq 0x190(%rbx), %rax
movq (%rax,%rbp,8), %rax
movq (%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq %r15, %rsi
callq 0x1a1ca
addl %r13d, (%rax)
movq 0x198(%rbx), %rax
movq (%rax,%rbp,8), %rax
movq (%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq %r15, %rsi
callq 0x1a1ca
addl %r13d, (%rax)
leaq 0x50(%rsp), %rdi
callq 0x55e8
leaq 0x100(%rsp), %r15
movq %r15, %rdi
callq 0x19c16
movq %r12, %rdi
callq 0x19c16
movq %r14, %rdi
callq 0x19c16
movq 0x18(%rsp), %rsi
addq $0x58, %rsi
jmp 0x13c6d
leaq 0x70(%rsp), %rdi
callq 0x19a38
leaq 0x88(%rsp), %rdi
callq 0x19a38
movq 0x28(%rsp), %rdi
callq 0x5240
movq %rax, %rcx
jmp 0x13be3
pushq $0x30
popq %rbx
leaq (%rsp,%rbx), %rdi
addq $0xa0, %rdi
callq 0x1c000
addq $-0x30, %rbx
cmpq $-0x30, %rbx
jne 0x13f2c
movb $0x1, %al
addq $0x2c8, %rsp            # imm = 0x2C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x218(%rsp), %rdi
callq 0x19a38
jmp 0x13f6f
movq %rax, %rbx
leaq 0x270(%rsp), %rdi
callq 0x19c16
jmp 0x1407b
jmp 0x13f91
jmp 0x13f91
jmp 0x13f91
jmp 0x13f91
movq %rax, %rbx
jmp 0x1406e
movq %rax, %rbx
jmp 0x1407b
movq %rax, %rbx
leaq 0x1f8(%rsp), %rdi
callq 0x55e8
jmp 0x1402e
jmp 0x1402b
movq %rax, %rbx
jmp 0x13fd7
movq %rax, %rbx
jmp 0x14045
movq %rax, %rbx
jmp 0x1400a
movq %rax, %rbx
jmp 0x13fe4
movq %rax, %rbx
leaq 0x1f8(%rsp), %rdi
callq 0x55e8
leaq 0x1d8(%rsp), %rdi
callq 0x55e8
leaq 0x1b8(%rsp), %rdi
callq 0x55e8
jmp 0x13ffd
jmp 0x1402b
movq %rax, %rbx
jmp 0x14017
movq %rax, %rbx
leaq 0x198(%rsp), %rdi
callq 0x55e8
leaq 0x178(%rsp), %rdi
callq 0x55e8
leaq 0x158(%rsp), %rdi
jmp 0x14033
movq %rax, %rbx
jmp 0x14038
movq %rax, %rbx
jmp 0x14064
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x55e8
leaq 0x100(%rsp), %rdi
callq 0x19c16
leaq 0x218(%rsp), %rdi
callq 0x19c16
jmp 0x14057
movq %rax, %rbx
leaq 0x270(%rsp), %rdi
callq 0x19c16
leaq 0x70(%rsp), %rdi
callq 0x19a38
leaq 0x88(%rsp), %rdi
callq 0x19a38
pushq $0x30
popq %r14
leaq (%rsp,%r14), %rdi
addq $0xa0, %rdi
callq 0x1c000
addq $-0x30, %r14
cmpq $-0x30, %r14
jne 0x1407f
movq %rbx, %rdi
callq 0x5540
nop