// File: /workspace/github_cpp_makefile_star2_O2/medvedevgroup[P]varmatch/src/wholegenome.cpp
// Function: WholeGenome::SingleThreadClustering(int)
void WholeGenome::SingleThreadClustering(int chr_id) {
	int ins_len[2] = { 0 };
	int del_len[2] = { 0 };
	int c_start = 0;
	int c_end = 0;
    sort(ref_variant_by_chrid[chr_id]->begin(), ref_variant_by_chrid[chr_id]->end());
    sort(que_variant_by_chrid[chr_id]->begin(), que_variant_by_chrid[chr_id]->end());
    int ref_size = ref_variant_by_chrid[chr_id]->size();
    int que_size = que_variant_by_chrid[chr_id]->size();
    //dout << chr_id << "," << ref_size << "," << que_size << endl;

    int ref_index = 0;
    int que_index = 0;
    bool not_first = false;
    DiploidVariant snp;
    vector<VariantIndicator> vi_list;
    while (ref_index < ref_size || que_index < que_size) {
		bool take_que = true;
		if(ref_index < ref_size && que_index < que_size){
            if(ref_variant_by_chrid[chr_id]->at(ref_index).pos < que_variant_by_chrid[chr_id]->at(que_index).pos){
                take_que = false;
            }
		}else if(ref_index < ref_size){
            take_que = false;
		}
        int var_index;
		if(take_que){

            snp = que_variant_by_chrid[chr_id]->at(que_index);
            //cout << "q |" << que_index << "," << snp.pos << endl;
            var_index = que_index;
            que_index++;
		}else{
            snp = ref_variant_by_chrid[chr_id]->at(ref_index);
            //cout << "r |" << ref_index << "," << snp.pos << endl;
            var_index = ref_index;
            ref_index++;
		}
		// check if need to separator clusters
		if (not_first) {
			c_end = snp.pos;
			if (c_end - c_start >= 2) {
                int separator_length = c_end - c_start;
				string separator = genome_sequences[chr_id].substr(c_start, separator_length);
				int max_change = max(ins_len[0] + del_len[1], ins_len[1] + del_len[0]);
				bool separate_cluster = false;
				if(max_change == 0){
                    separate_cluster = true;
				}
				else if (separator_length > 2 * max_change &&
					(separator_length > MAX_REPEAT_LEN || !CheckTandemRepeat(separator, max_change)))
				{
				    separate_cluster = true;
				}

				if(separate_cluster){
                    variant_cluster_by_chrid[chr_id]->push_back(vi_list);
                    vi_list.clear();
					ins_len[0] = 0;
					del_len[0] = 0;
					ins_len[1] = 0;
					del_len[1] = 0;
					c_start = 0; // re-assign c_start
				}
			}
		}
		c_start = max(c_start, snp.pos + (int)snp.ref.length() );
        VariantIndicator current_variant_indicator(chr_id, var_index, !take_que);
        vi_list.push_back(current_variant_indicator);
		//cluster_vars_map[cluster_index].push_back(snp);
		if(!not_first) not_first = true;
		int ref_length = (int)(snp.ref.length());
		int flag = 0;
        if(snp.flag) flag = 1;
//        DiploidVariant snp = front_cluster[k];
//        int rq = snp.flag;
        ins_len[flag] += snp.mil;
        del_len[flag] += snp.mdl;
	}
    if(vi_list.size() > 0){
        variant_cluster_by_chrid[chr_id]->push_back(vi_list);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rdi, %r14
movl %esi, 0x28(%rsp)
andq $0x0, 0x48(%rsp)
andq $0x0, 0x40(%rsp)
movq 0x148(%rdi), %rax
movslq %esi, %rbx
movq (%rax,%rbx,8), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
callq 0x1e3ed
movq 0x150(%r14), %rax
movq (%rax,%rbx,8), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
callq 0x1e3ed
movq 0x148(%r14), %rax
movq 0x150(%r14), %rcx
movq (%rax,%rbx,8), %rdx
movq 0x8(%rdx), %rax
subq (%rdx), %rax
pushq $0x58
popq %rsi
cqto
idivq %rsi
movq %rax, %r12
movq (%rcx,%rbx,8), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
cqto
idivq %rsi
movq %rax, 0x70(%rsp)
leaq 0x131d3(%rip), %rsi     # 0x225aa
leaq 0xe0(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x72ee
leaq 0x131ba(%rip), %rsi     # 0x225aa
leaq 0x88(%rsp), %rdi
leaq 0x2f(%rsp), %rdx
callq 0x72ee
leaq 0xa8(%rsp), %rdi
leaq 0x13199(%rip), %rsi     # 0x225aa
leaq 0x2e(%rsp), %rdx
callq 0x72ee
leaq 0xc8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
pushq $0x2
popq %rdx
leaq 0x2d(%rsp), %rcx
callq 0x1a34c
andl $0x0, 0x18(%rsp)
andl $0x0, 0x10(%rsp)
andl $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
leaq 0x100(%rsp), %rdi
pushq $-0x1
popq %rsi
leaq 0xe0(%rsp), %rdx
leaq 0xc8(%rsp), %rcx
xorps %xmm0, %xmm0
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x19b60
leaq 0xc8(%rsp), %rdi
callq 0xadcc
pushq $0x20
popq %rbx
leaq (%rsp,%rbx), %rdi
addq $0x88, %rdi
callq 0x55e8
addq $-0x20, %rbx
cmpq $-0x20, %rbx
jne 0xf484
leaq 0xe0(%rsp), %rdi
callq 0x55e8
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %r14, 0x38(%rsp)
leaq 0x118(%r14), %rax
movq %rax, 0x78(%rsp)
xorl %ebx, %ebx
xorl %r13d, %r13d
movq $0x0, 0x30(%rsp)
xorl %r15d, %r15d
movq %r12, 0x80(%rsp)
cmpl %r12d, %r15d
jl 0xf4f9
movq 0x30(%rsp), %rax
cmpl 0x70(%rsp), %eax
jge 0xf740
cmpl %r12d, %r15d
jge 0xf552
movq 0x30(%rsp), %rax
cmpl 0x70(%rsp), %eax
jge 0xf552
movq 0x38(%rsp), %r12
movq 0x148(%r12), %rax
movslq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movslq %r15d, %rbp
movq %rbp, %rsi
callq 0x1a386
movl (%rax), %r14d
movq 0x150(%r12), %rax
movslq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movslq 0x30(%rsp), %r12
movq %r12, %rsi
callq 0x1a386
cmpl (%rax), %r14d
jl 0xf55a
jmp 0xf59b
cmpl %r12d, %r15d
jge 0xf596
movslq %r15d, %rbp
movq 0x38(%rsp), %rax
movq 0x148(%rax), %rax
movslq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq %rbp, %rsi
callq 0x1a386
leaq 0x100(%rsp), %rdi
movq %rax, %rsi
callq 0x1a3a4
movl %r15d, %r12d
leal 0x1(%r15), %eax
movb $0x1, %bpl
movl %eax, %r15d
jmp 0xf5d9
movslq 0x30(%rsp), %r12
movq 0x38(%rsp), %rax
movq 0x150(%rax), %rax
movslq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq %r12, %rsi
callq 0x1a386
leaq 0x100(%rsp), %rdi
movq %rax, %rsi
callq 0x1a3a4
movq 0x30(%rsp), %rax
movl %eax, %r12d
incl %eax
xorl %ebp, %ebp
movq %rax, 0x30(%rsp)
movl 0x100(%rsp), %r14d
testb $0x1, %r13b
je 0xf6d0
movl %r14d, %r13d
subl %ebx, %r13d
cmpl $0x2, %r13d
jl 0xf6d0
movq 0x78(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x19a5a
movl %ebx, %edx
movl %r13d, %ecx
leaq 0x88(%rsp), %rdi
movq %rax, %rsi
callq 0x5320
movl 0x40(%rsp), %eax
movl 0x44(%rsp), %ecx
addl 0x48(%rsp), %ecx
addl 0x4c(%rsp), %eax
cmpl %eax, %ecx
cmovgl %ecx, %eax
testl %eax, %eax
je 0xf67d
addl %eax, %eax
cmpl %eax, %r13d
jle 0xf6bb
cmpl $0x3e8, %r13d           # imm = 0x3E8
ja 0xf67d
leaq 0x158(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x53f0
leaq 0x158(%rsp), %r13
movq %r13, %rsi
callq 0xf844
movl %eax, %r14d
movq %r13, %rdi
callq 0x55e8
testb %r14b, %r14b
jne 0xf6bb
movq 0x38(%rsp), %rax
movq 0x158(%rax), %rax
movslq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x50(%rsp), %rsi
callq 0x1abfe
movq 0x50(%rsp), %rax
cmpq %rax, 0x58(%rsp)
je 0xf6ad
movq %rax, 0x58(%rsp)
andq $0x0, 0x48(%rsp)
andq $0x0, 0x40(%rsp)
xorl %ebx, %ebx
leaq 0x88(%rsp), %rdi
callq 0x55e8
movl 0x100(%rsp), %r14d
movq 0x110(%rsp), %r13
movb 0x28(%rsp), %al
movb %al, 0x88(%rsp)
movl %r12d, 0x8c(%rsp)
movb %bpl, 0x90(%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x1ac2a
addl %r13d, %r14d
cmpl %r14d, %ebx
cmovlel %r14d, %ebx
movzbl 0x14c(%rsp), %eax
movl 0x144(%rsp), %ecx
movl 0x148(%rsp), %edx
shll $0x2, %eax
addl %edx, 0x48(%rsp,%rax)
addl %ecx, 0x40(%rsp,%rax)
movb $0x1, %r13b
movq 0x80(%rsp), %r12
jmp 0xf4e5
movq 0x58(%rsp), %rax
cmpq 0x50(%rsp), %rax
je 0xf76b
movq 0x38(%rsp), %rax
movq 0x158(%rax), %rax
movslq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x50(%rsp), %rsi
callq 0x1abfe
leaq 0x50(%rsp), %rdi
callq 0x1f18e
leaq 0x100(%rsp), %rdi
callq 0x19c16
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xf821
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0xadcc
jmp 0xf7ae
movq %rax, %rbx
pushq $0x20
popq %r14
leaq (%rsp,%r14), %rdi
addq $0x88, %rdi
callq 0x55e8
addq $-0x20, %r14
cmpq $-0x20, %r14
jne 0xf7b2
jmp 0xf7e3
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x55e8
jmp 0xf7e3
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x55e8
jmp 0xf83b
movq %rax, %rbx
jmp 0xf83b
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
callq 0x55e8
jmp 0xf80c
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x55e8
jmp 0xf824
jmp 0xf821
jmp 0xf821
jmp 0xf821
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x1f18e
leaq 0x100(%rsp), %rdi
callq 0x19c16
movq %rbx, %rdi
callq 0x5540
nop