// File: /workspace/github_cpp_makefile_star2_O1/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 $0x158, %rsp            # imm = 0x158
movq %rdi, %rbx
movl %esi, 0x8(%rsp)
xorl %eax, %eax
movq %rax, 0x50(%rsp)
movq %rax, 0x48(%rsp)
movq 0x148(%rdi), %rax
movslq %esi, %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %r14
movq 0x8(%rax), %r15
movabsq $0x2e8ba2e8ba2e8ba3, %rcx # imm = 0x2E8BA2E8BA2E8BA3
cmpq %r15, %r14
je 0x14769
movq %r15, %rax
subq %r14, %rax
sarq $0x3, %rax
imulq %rcx, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x2cdcd
movq %r14, %rdi
movq %r15, %rsi
callq 0x2cef9
movq 0x150(%rbx), %rax
movslq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %r14
movq 0x8(%rax), %r15
cmpq %r15, %r14
je 0x147c0
movq %r15, %rax
subq %r14, %rax
sarq $0x3, %rax
movabsq $0x2e8ba2e8ba2e8ba3, %rcx # imm = 0x2E8BA2E8BA2E8BA3
imulq %rcx, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x2cdcd
movq %r14, %rdi
movq %r15, %rsi
callq 0x2cef9
movslq 0x8(%rsp), %rax
movq 0x148(%rbx), %rcx
movq %rbx, 0x40(%rsp)
movq 0x150(%rbx), %rdx
movq (%rcx,%rax,8), %rcx
movq 0x8(%rcx), %rbx
subq (%rcx), %rbx
movq (%rdx,%rax,8), %rax
movq 0x8(%rax), %rbp
subq (%rax), %rbp
leaq 0xd8(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x1cdc0(%rip), %r14     # 0x315c2
leaq 0xc8(%rsp), %rdi
movq %r14, %rsi
movq %r14, %rdx
callq 0xc45c
leaq 0x68(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x58(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %r14, %rdx
callq 0xc45c
leaq 0x78(%rsp), %r14
leaq 0x88(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x1cd79(%rip), %rdx     # 0x315c2
movq %r14, %rdi
movq %rdx, %rsi
callq 0xc45c
leaq 0x140(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x10(%rsp), %rcx
movl $0x2, %edx
callq 0x292e8
xorl %eax, %eax
leaq 0xe8(%rsp), %rdi
leaq 0xc8(%rsp), %rdx
leaq 0x140(%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 0x28a76
addq $0x20, %rsp
shrq $0x3, %rbx
imull $0xba2e8ba3, %ebx, %eax # imm = 0xBA2E8BA3
movl %eax, 0x3c(%rsp)
shrq $0x3, %rbp
imull $0xba2e8ba3, %ebp, %eax # imm = 0xBA2E8BA3
movl %eax, 0xc(%rsp)
leaq 0x140(%rsp), %rdi
callq 0xe7ca
movq $-0x40, %rbx
movq -0x10(%r13), %rdi
cmpq %rdi, %r13
je 0x148ea
movq (%r13), %rsi
incq %rsi
callq 0x5340
addq $-0x20, %r13
addq $0x20, %rbx
jne 0x148d5
movq 0xc8(%rsp), %rdi
cmpq %r12, %rdi
je 0x14911
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x5340
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
setg %al
cmpl $0x0, 0x3c(%rsp)
setg %bl
movq 0x40(%rsp), %r8
jg 0x14942
testl %ecx, %ecx
jle 0x14d46
leaq 0x118(%r8), %rcx
movq %rcx, 0xb8(%rsp)
xorl %ebp, %ebp
xorl %r15d, %r15d
xorl %r10d, %r10d
movq $0x0, 0x30(%rsp)
testb $0x1, %bl
je 0x149df
testb $0x1, %al
je 0x149df
movq 0x148(%r8), %rax
movslq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
movslq %ebp, %rsi
movq (%rdx), %rax
movq 0x8(%rdx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $0x2e8ba2e8ba2e8ba3, %r9 # imm = 0x2E8BA2E8BA2E8BA3
imulq %r9, %rdx
cmpq %rsi, %rdx
jbe 0x14dd7
movq 0x150(%r8), %rdx
movq (%rdx,%rcx,8), %rdx
movslq %r15d, %rcx
movq (%rdx), %rdi
movq 0x8(%rdx), %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
imulq %r9, %rdx
cmpq %rcx, %rdx
jbe 0x14dd4
imulq $0x58, %rsi, %rdx
movl (%rax,%rdx), %eax
imulq $0x58, %rcx, %rcx
cmpl (%rdi,%rcx), %eax
setge %bl
jmp 0x149e2
xorb $0x1, %bl
movq %r10, 0xc0(%rsp)
movslq 0x8(%rsp), %rax
testb $0x1, %bl
je 0x14a8f
movq 0x150(%r8), %rcx
movq (%rcx,%rax,8), %rax
movslq %r15d, %rsi
movq (%rax), %r12
movq 0x8(%rax), %rdx
subq %r12, %rdx
sarq $0x3, %rdx
movabsq $0x2e8ba2e8ba2e8ba3, %rax # imm = 0x2E8BA2E8BA2E8BA3
imulq %rax, %rdx
cmpq %rsi, %rdx
jbe 0x14dc6
movq %r15, %r14
imulq $0x58, %rsi, %r13
leaq (%r12,%r13), %rsi
addq $0x8, %rsi
movl -0x8(%rsi), %eax
movl %eax, 0xe8(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x51c0
addq %r13, %r12
leaq 0x28(%r12), %rsi
leaq 0x110(%rsp), %rdi
callq 0xa67a
movq 0x50(%r12), %rax
leaq 0x128(%rsp), %rcx
movq %rax, 0x10(%rcx)
movups 0x40(%r12), %xmm0
movups %xmm0, (%rcx)
movl %r14d, %r12d
leal 0x1(%r14), %eax
movl %eax, %r15d
jmp 0x14b26
movq 0x148(%r8), %rcx
movq (%rcx,%rax,8), %rax
movslq %ebp, %rsi
movq (%rax), %r12
movq 0x8(%rax), %rdx
subq %r12, %rdx
sarq $0x3, %rdx
movabsq $0x2e8ba2e8ba2e8ba3, %rax # imm = 0x2E8BA2E8BA2E8BA3
imulq %rax, %rdx
cmpq %rsi, %rdx
jbe 0x14dc6
movq %r15, %r14
movq %rbp, %r15
imulq $0x58, %rsi, %r13
leaq (%r12,%r13), %rsi
addq $0x8, %rsi
movl -0x8(%rsi), %eax
movl %eax, 0xe8(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x51c0
addq %r13, %r12
leaq 0x28(%r12), %rsi
leaq 0x110(%rsp), %rdi
callq 0xa67a
movq 0x50(%r12), %rax
leaq 0x128(%rsp), %rcx
movq %rax, 0x10(%rcx)
movups 0x40(%r12), %xmm0
movups %xmm0, (%rcx)
movl %r15d, %r12d
leal 0x1(%r15), %eax
movl %eax, %ebp
movq %r14, %r15
movq 0xc0(%rsp), %r14
testb $0x1, %r14b
je 0x14c7f
movl 0xe8(%rsp), %r13d
subl 0x30(%rsp), %r13d
cmpl $0x2, %r13d
jl 0x14c7f
movq 0xb8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x28988
movl 0x30(%rsp), %edx
movl %r13d, %ecx
leaq 0x58(%rsp), %rdi
movq %rax, %rsi
callq 0x5360
movl 0x48(%rsp), %eax
movl 0x4c(%rsp), %ecx
addl 0x50(%rsp), %ecx
addl 0x54(%rsp), %eax
cmpl %eax, %ecx
cmovgl %ecx, %eax
testl %eax, %eax
je 0x14c1e
addl %eax, %eax
cmpl %eax, %r13d
jle 0x14ba9
cmpl $0x3e8, %r13d           # imm = 0x3E8
jle 0x14bb0
movb $0x1, %r13b
xorl %eax, %eax
jmp 0x14bf0
xorl %eax, %eax
xorl %r13d, %r13d
jmp 0x14bf0
leaq 0xa8(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
addq %rsi, %rdx
leaq 0x98(%rsp), %rdi
callq 0x112ce
leaq 0x98(%rsp), %rsi
callq 0x14f18
movl %eax, %r13d
xorb $0x1, %r13b
movb $0x1, %al
testb %al, %al
je 0x14c19
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x14c19
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x5340
testb %r13b, %r13b
je 0x14c63
movq 0x40(%rsp), %rax
movq 0x158(%rax), %rax
movslq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x10(%rsp), %rsi
callq 0x29be0
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x14c4e
movq %rax, 0x18(%rsp)
xorl %eax, %eax
movq %rax, 0x50(%rsp)
movq $0x0, 0x30(%rsp)
movq %rax, 0x48(%rsp)
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x14c7f
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x5340
movq 0x30(%rsp), %rax
movl 0xe8(%rsp), %ecx
addl 0xf8(%rsp), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movq %rcx, 0x30(%rsp)
movb 0x8(%rsp), %al
notb %bl
andb $0x1, %bl
movb %al, 0x58(%rsp)
movl %r12d, 0x5c(%rsp)
movb %bl, 0x60(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x14cde
movl 0x60(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x58(%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x18(%rsp)
movq 0x40(%rsp), %r8
movl 0xc(%rsp), %edx
jmp 0x14cf6
leaq 0x10(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x2f126
movq 0x40(%rsp), %r8
movl 0xc(%rsp), %edx
testb $0x1, %r14b
movzbl %r14b, %eax
movl $0x1, %ecx
cmovel %ecx, %eax
movl %eax, %r10d
movzbl 0x134(%rsp), %eax
movl 0x130(%rsp), %ecx
shll $0x2, %eax
addl %ecx, 0x50(%rsp,%rax)
movl 0x12c(%rsp), %ecx
addl %ecx, 0x48(%rsp,%rax)
cmpl %edx, %r15d
setl %al
cmpl 0x3c(%rsp), %ebp
setl %bl
jl 0x14962
cmpl %edx, %r15d
jl 0x14962
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
je 0x14d6c
movq 0x158(%r8), %rax
movslq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x10(%rsp), %rsi
callq 0x29be0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x14d83
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x5340
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
callq 0xe7ca
movq -0x20(%rbx), %rdi
leaq 0x100(%rsp), %rax
cmpq %rax, %rdi
je 0x14db4
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x5340
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1ccf7(%rip), %rdi     # 0x31ac4
xorl %eax, %eax
callq 0x5490
movq %rcx, %rsi
leaq 0x1cce6(%rip), %rdi     # 0x31ac4
xorl %eax, %eax
callq 0x5490
movq %rax, %rbx
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x14ec3
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x5340
jmp 0x14ec3
jmp 0x14ee9
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0xe7ca
jmp 0x14e30
movq %rax, %rbx
xorl %ebp, %ebp
movq 0x78(%rsp,%rbp), %rdi
movq %r13, %rax
addq %rbp, %rax
cmpq %rdi, %rax
je 0x14e4d
movq (%rax), %rsi
incq %rsi
callq 0x5340
addq $-0x20, %rbp
cmpq $-0x40, %rbp
jne 0x14e32
movb $0x1, %al
jmp 0x14e6a
movq %rax, %rbx
xorl %eax, %eax
jmp 0x14e6a
movq %rax, %rbx
xorl %eax, %eax
movq %r15, %r14
cmpq %r14, %r15
sete %cl
orb %al, %cl
jne 0x14ea1
addq $-0x10, %r14
leaq 0x58(%rsp), %r15
movq -0x10(%r14), %rdi
cmpq %rdi, %r14
je 0x14e91
movq (%r14), %rsi
incq %rsi
callq 0x5340
leaq -0x20(%r14), %rax
addq $-0x10, %r14
cmpq %r15, %r14
movq %rax, %r14
jne 0x14e7d
movq 0xc8(%rsp), %rdi
cmpq %r12, %rdi
je 0x14f10
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x5340
jmp 0x14f10
movq %rax, %rbx
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x14eec
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x5340
jmp 0x14eec
jmp 0x14ee9
jmp 0x14ee9
jmp 0x14ee9
jmp 0x14ee9
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x14f03
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x5340
leaq 0xe8(%rsp), %rdi
callq 0x28b44
movq %rbx, %rdi
callq 0x55c0