// File: /workspace/github_cpp_makefile_star2_O3/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 $0x148, %rsp            # imm = 0x148
movl %esi, 0xc(%rsp)
xorl %eax, %eax
movq %rax, 0x48(%rsp)
movq %rax, 0x40(%rsp)
movq %rdi, 0x30(%rsp)
movq 0x148(%rdi), %rax
movslq %esi, %r12
movq (%rax,%r12,8), %rax
movq (%rax), %r14
movq 0x8(%rax), %r15
movabsq $0x2e8ba2e8ba2e8ba3, %rbx # imm = 0x2E8BA2E8BA2E8BA3
cmpq %r15, %r14
je 0x146c9
movq %r15, %rax
subq %r14, %rax
sarq $0x3, %rax
imulq %rbx, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x2c82b
movq %r14, %rdi
movq %r15, %rsi
callq 0x2c957
movq 0x30(%rsp), %r13
movq 0x150(%r13), %rax
movq (%rax,%r12,8), %rax
movq (%rax), %r14
movq 0x8(%rax), %r15
cmpq %r15, %r14
je 0x1472a
movq %r15, %rax
subq %r14, %rax
sarq $0x3, %rax
imulq %rbx, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x2c82b
movq %r14, %rdi
movq %r15, %rsi
callq 0x2c957
movq 0x150(%r13), %rax
movq (%rax,%r12,8), %rax
movq (%rax), %r14
movq 0x8(%rax), %rbx
jmp 0x1472d
movq %r14, %rbx
movq 0x148(%r13), %rax
movq (%rax,%r12,8), %rax
movq 0x8(%rax), %r13
subq (%rax), %r13
leaq 0xc8(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x1ce9f(%rip), %r15     # 0x315f2
leaq 0xb8(%rsp), %rdi
movq %r15, %rsi
movq %r15, %rdx
callq 0xc3da
leaq 0x60(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
movq %r15, %rdx
callq 0xc3da
leaq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x1ce5b(%rip), %rdx     # 0x315f2
movq %rdx, %rsi
callq 0xc3da
leaq 0x130(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x10(%rsp), %rcx
movl $0x2, %edx
callq 0x28e82
xorl %eax, %eax
leaq 0xd8(%rsp), %rdi
leaq 0xb8(%rsp), %rdx
leaq 0x130(%rsp), %rcx
xorps %xmm0, %xmm0
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
pushq %rax
pushq %rax
pushq %rax
callq 0x2860c
addq $0x20, %rsp
shrq $0x3, %r13
imull $0xba2e8ba3, %r13d, %eax # imm = 0xBA2E8BA3
movl %eax, 0x3c(%rsp)
subq %r14, %rbx
shrq $0x3, %rbx
imull $0xba2e8ba3, %ebx, %r14d # imm = 0xBA2E8BA3
leaq 0x130(%rsp), %rdi
callq 0xe71c
movq $-0x40, %rbx
movq -0x10(%r15), %rdi
cmpq %rdi, %r15
je 0x14835
movq (%r15), %rsi
incq %rsi
callq 0x5340
addq $-0x20, %r15
addq $0x20, %rbx
jne 0x14821
movq 0xb8(%rsp), %rdi
cmpq %r12, %rdi
je 0x1485c
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x5340
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
testl %r14d, %r14d
setg %al
cmpl $0x0, 0x3c(%rsp)
setg %cl
movq 0x30(%rsp), %r8
jg 0x1488b
testl %r14d, %r14d
jle 0x14c72
leaq 0x118(%r8), %rdx
movq %rdx, 0xb0(%rsp)
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %ebx, %ebx
movl %r14d, 0x38(%rsp)
testb $0x1, %cl
je 0x1492a
testb $0x1, %al
je 0x1492a
movq 0x148(%r8), %rcx
movslq 0xc(%rsp), %rax
movq (%rcx,%rax,8), %rcx
movslq %r13d, %rsi
movq (%rcx), %r14
movq 0x8(%rcx), %rdx
subq %r14, %rdx
sarq $0x3, %rdx
movabsq $0x2e8ba2e8ba2e8ba3, %rdi # imm = 0x2E8BA2E8BA2E8BA3
imulq %rdi, %rdx
cmpq %rsi, %rdx
jbe 0x14cd5
movq 0x150(%r8), %rcx
movq (%rcx,%rax,8), %rcx
movslq %ebp, %rax
movq (%rcx), %r15
movq 0x8(%rcx), %rcx
subq %r15, %rcx
sarq $0x3, %rcx
imulq %rdi, %rcx
cmpq %rax, %rcx
jbe 0x14ccf
imulq $0x58, %rsi, %rdi
movl (%r14,%rdi), %edi
imulq $0x58, %rax, %r8
cmpl (%r15,%r8), %edi
jl 0x14962
jmp 0x149f7
movslq 0xc(%rsp), %rax
testb $0x1, %cl
je 0x149cd
movq 0x148(%r8), %rcx
movq (%rcx,%rax,8), %rax
movq (%rax), %r14
movq 0x8(%rax), %rdx
movslq %r13d, %rsi
subq %r14, %rdx
sarq $0x3, %rdx
movabsq $0x2e8ba2e8ba2e8ba3, %rax # imm = 0x2E8BA2E8BA2E8BA3
imulq %rax, %rdx
cmpq %rsi, %rdx
jbe 0x14cb5
imulq $0x58, %rsi, %r15
leaq (%r14,%r15), %rsi
addq $0x8, %rsi
movl -0x8(%rsi), %eax
movl %eax, 0xd8(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x51c0
addq %r15, %r14
leaq 0x28(%r14), %rsi
leaq 0x100(%rsp), %rdi
callq 0xa5da
movq 0x50(%r14), %rax
leaq 0xe0(%rsp), %rcx
movq %rax, 0x48(%rcx)
movups 0x40(%r14), %xmm0
movups %xmm0, 0x38(%rcx)
movl %r13d, %r15d
leal 0x1(%r13), %eax
movb $0x1, %r14b
movl %eax, %r13d
jmp 0x14a5b
movq 0x150(%r8), %rcx
movq (%rcx,%rax,8), %rax
movq (%rax), %r15
movq 0x8(%rax), %rcx
movslq %ebp, %rax
subq %r15, %rcx
sarq $0x3, %rcx
movabsq $0x2e8ba2e8ba2e8ba3, %rdx # imm = 0x2E8BA2E8BA2E8BA3
imulq %rdx, %rcx
cmpq %rax, %rcx
jbe 0x14cbb
imulq $0x58, %rax, %r14
leaq (%r15,%r14), %rsi
addq $0x8, %rsi
movl -0x8(%rsi), %eax
movl %eax, 0xd8(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x51c0
addq %r14, %r15
leaq 0x28(%r15), %rsi
leaq 0x100(%rsp), %rdi
callq 0xa5da
movq 0x50(%r15), %rax
leaq 0xe0(%rsp), %rcx
movq %rax, 0x48(%rcx)
movups 0x40(%r15), %xmm0
movups %xmm0, 0x38(%rcx)
movl %ebp, %r15d
leal 0x1(%rbp), %eax
xorl %r14d, %r14d
movl %eax, %ebp
movl 0xd8(%rsp), %eax
testb $0x1, %r12b
je 0x14b90
movl %eax, %r12d
subl %ebx, %r12d
cmpl $0x2, %r12d
jl 0x14b90
movq 0xb0(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x2851c
movl %ebx, %edx
movl %r12d, %ecx
leaq 0x50(%rsp), %rdi
movq %rax, %rsi
callq 0x5360
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 0x14b31
addl %eax, %eax
cmpl %eax, %r12d
jle 0x14b6d
cmpl $0x3e8, %r12d           # imm = 0x3E8
ja 0x14b31
leaq 0xa0(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
addq %rsi, %rdx
leaq 0x90(%rsp), %rdi
callq 0x1123c
leaq 0x90(%rsp), %rsi
callq 0x14df2
movl %eax, %r12d
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x14b2c
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x5340
testb %r12b, %r12b
jne 0x14b6d
movq 0x30(%rsp), %rax
movq 0x158(%rax), %rax
movslq 0xc(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x10(%rsp), %rsi
callq 0x29748
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x14b61
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
movq %rbx, 0x48(%rsp)
movq %rbx, 0x40(%rsp)
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x14b89
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x5340
movl 0xd8(%rsp), %eax
movl %ebx, %ecx
addl 0xe8(%rsp), %eax
cmpl %eax, %ebx
movl %eax, %ebx
cmovgl %ecx, %ebx
movb 0xc(%rsp), %al
movb %al, 0x50(%rsp)
movl %r15d, 0x54(%rsp)
movb %r14b, 0x58(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x14bde
movl 0x58(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x50(%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x18(%rsp)
movq 0x30(%rsp), %r8
movl 0x38(%rsp), %esi
jmp 0x14bf6
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x2f024
movq 0x30(%rsp), %r8
movl 0x38(%rsp), %esi
movzbl 0x124(%rsp), %eax
movl 0x11c(%rsp), %ecx
movl 0x120(%rsp), %edx
shll $0x2, %eax
addl %edx, 0x48(%rsp,%rax)
addl %ecx, 0x40(%rsp,%rax)
cmpl %esi, %ebp
setl %al
movb $0x1, %r12b
cmpl 0x3c(%rsp), %r13d
setl %cl
jl 0x148a9
cmpl %esi, %ebp
jl 0x148a9
movq 0x10(%rsp), %rdi
cmpq %rdi, 0x18(%rsp)
je 0x14c60
movq 0x158(%r8), %rax
movslq 0xc(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x10(%rsp), %rsi
callq 0x29748
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x14c72
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x5340
leaq 0x100(%rsp), %rbx
movq %rbx, %rdi
callq 0xe71c
movq -0x20(%rbx), %rdi
leaq 0xf0(%rsp), %rax
cmpq %rax, %rdi
je 0x14ca3
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x5340
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %rax
movq %rdx, %rcx
leaq 0x1ce32(%rip), %rdi     # 0x31af4
movq %rax, %rsi
movq %rcx, %rdx
xorl %eax, %eax
callq 0x5490
movq %rax, %rsi
movq %rcx, %rdx
leaq 0x1ce18(%rip), %rdi     # 0x31af4
xorl %eax, %eax
callq 0x5490
jmp 0x14dc2
movq %rax, %rbx
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x14d9c
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x5340
jmp 0x14d9c
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0xe71c
jmp 0x14d2e
movq %rax, %rbx
xorl %r14d, %r14d
movq 0x70(%rsp,%r14), %rdi
leaq (%r15,%r14), %rax
cmpq %rdi, %rax
je 0x14d4a
movq (%rax), %rsi
incq %rsi
callq 0x5340
addq $-0x20, %r14
cmpq $-0x40, %r14
jne 0x14d31
jmp 0x14d7a
movq %rax, %rbx
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x14d7a
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x5340
jmp 0x14d7a
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
cmpq %r12, %rdi
je 0x14de9
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x5340
jmp 0x14de9
movq %rax, %rbx
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x14dc5
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x5340
jmp 0x14dc5
jmp 0x14dc2
jmp 0x14dc2
jmp 0x14dc2
jmp 0x14dc2
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x14ddc
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x5340
leaq 0xd8(%rsp), %rdi
callq 0x286da
movq %rbx, %rdi
callq 0x55c0
nop