// File: 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
movq %rsp, %rbp
subq $0x2f0, %rsp            # imm = 0x2F0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x210(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movq 0x148(%rax), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x33d60
movq %rax, %rcx
movq -0x210(%rbp), %rax
movq %rcx, -0x30(%rbp)
movq 0x148(%rax), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x33d90
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x33d20
movq -0x210(%rbp), %rax
movq 0x150(%rax), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x33d60
movq %rax, %rcx
movq -0x210(%rbp), %rax
movq %rcx, -0x40(%rbp)
movq 0x150(%rax), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x33d90
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x33d20
movq -0x210(%rbp), %rax
movq 0x148(%rax), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x335b0
movq %rax, %rcx
movq -0x210(%rbp), %rax
movl %ecx, -0x4c(%rbp)
movq 0x150(%rax), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x335b0
movl %eax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
movb $0x0, -0x59(%rbp)
leaq -0xd9(%rbp), %rdi
movq %rdi, -0x208(%rbp)
callq 0x6760
movq -0x208(%rbp), %rdx
leaq 0x36abc(%rip), %rsi     # 0x5559e
leaq -0xd8(%rbp), %rdi
callq 0x9680
jmp 0x1eaf0
movb $0x1, -0x163(%rbp)
leaq -0x158(%rbp), %rax
movq %rax, -0x220(%rbp)
movq %rax, -0x160(%rbp)
leaq -0x161(%rbp), %rdi
movq %rdi, -0x218(%rbp)
callq 0x6760
movq -0x220(%rbp), %rdi
movq -0x218(%rbp), %rdx
leaq 0x36a6a(%rip), %rsi     # 0x5559e
callq 0x9680
jmp 0x1eb3b
leaq -0x138(%rbp), %rax
movq %rax, -0x230(%rbp)
movq %rax, -0x160(%rbp)
leaq -0x162(%rbp), %rdi
movq %rdi, -0x228(%rbp)
callq 0x6760
movq -0x230(%rbp), %rdi
movq -0x228(%rbp), %rdx
leaq 0x36a26(%rip), %rsi     # 0x5559e
callq 0x9680
jmp 0x1eb7f
movb $0x0, -0x163(%rbp)
leaq -0x158(%rbp), %rax
movq %rax, -0x118(%rbp)
movq $0x2, -0x110(%rbp)
leaq -0x164(%rbp), %rdi
movq %rdi, -0x238(%rbp)
callq 0x13420
movq -0x238(%rbp), %rcx
movq -0x118(%rbp), %rsi
movq -0x110(%rbp), %rdx
leaq -0x108(%rbp), %rdi
callq 0x34770
jmp 0x1ebd5
movq %rsp, %rax
movl $0x0, 0x18(%rax)
movl $0x0, 0x10(%rax)
movl $0x0, 0x8(%rax)
movl $0x0, (%rax)
leaq -0xb8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq -0xd8(%rbp), %rdx
leaq -0x108(%rbp), %rcx
xorl %r9d, %r9d
xorps %xmm0, %xmm0
movl %r9d, %r8d
callq 0x32ac0
jmp 0x1ec1d
leaq -0x108(%rbp), %rdi
callq 0x13370
leaq -0x164(%rbp), %rdi
callq 0x14fa0
leaq -0x158(%rbp), %rax
movq %rax, -0x248(%rbp)
addq $0x40, %rax
movq %rax, -0x240(%rbp)
movq -0x240(%rbp), %rdi
addq $-0x20, %rdi
movq %rdi, -0x250(%rbp)
callq 0x6828
movq -0x248(%rbp), %rcx
movq -0x250(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x240(%rbp)
jne 0x1ec4e
leaq -0x162(%rbp), %rdi
callq 0x64b0
leaq -0x161(%rbp), %rdi
callq 0x64b0
leaq -0xd8(%rbp), %rdi
callq 0x6828
leaq -0xd9(%rbp), %rdi
callq 0x64b0
leaq -0x180(%rbp), %rdi
callq 0x363a0
movl -0x54(%rbp), %ecx
movb $0x1, %al
cmpl -0x4c(%rbp), %ecx
movb %al, -0x251(%rbp)
jl 0x1ecda
movl -0x58(%rbp), %eax
cmpl -0x50(%rbp), %eax
setl %al
movb %al, -0x251(%rbp)
movb -0x251(%rbp), %al
testb $0x1, %al
jne 0x1ece9
jmp 0x1f2fa
movb $0x1, -0x181(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x1eefd
movl -0x58(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x1eefd
movq -0x210(%rbp), %rax
movq 0x148(%rax), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movslq -0x54(%rbp), %rsi
callq 0x347f0
movq %rax, -0x260(%rbp)
jmp 0x1ed30
movq -0x210(%rbp), %rax
movq -0x260(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, -0x26c(%rbp)
movq 0x150(%rax), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movslq -0x58(%rbp), %rsi
callq 0x347f0
movq %rax, -0x268(%rbp)
jmp 0x1ed67
movl -0x26c(%rbp), %eax
movq -0x268(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x1eefb
movb $0x0, -0x181(%rbp)
jmp 0x1eefb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
jmp 0x1eed3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
jmp 0x1ee63
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
jmp 0x1ee57
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
jmp 0x1edff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x13370
leaq -0x164(%rbp), %rdi
callq 0x14fa0
leaq -0x158(%rbp), %rax
movq %rax, -0x280(%rbp)
addq $0x40, %rax
movq %rax, -0x278(%rbp)
movq -0x278(%rbp), %rdi
addq $-0x20, %rdi
movq %rdi, -0x288(%rbp)
callq 0x6828
movq -0x280(%rbp), %rcx
movq -0x288(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x278(%rbp)
jne 0x1ee24
jmp 0x1ee57
leaq -0x162(%rbp), %rdi
callq 0x64b0
leaq -0x161(%rbp), %rdi
callq 0x64b0
testb $0x1, -0x163(%rbp)
jne 0x1ee7a
jmp 0x1eec7
movq -0x160(%rbp), %rax
leaq -0x158(%rbp), %rcx
cmpq %rax, %rcx
movq %rax, -0x290(%rbp)
je 0x1eec5
movq -0x290(%rbp), %rdi
addq $-0x20, %rdi
movq %rdi, -0x298(%rbp)
callq 0x6828
movq -0x298(%rbp), %rax
leaq -0x158(%rbp), %rcx
cmpq %rcx, %rax
movq %rax, -0x290(%rbp)
jne 0x1ee94
jmp 0x1eec7
leaq -0xd8(%rbp), %rdi
callq 0x6828
leaq -0xd9(%rbp), %rdi
callq 0x64b0
jmp 0x1f36b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
jmp 0x1f353
jmp 0x1ef0e
movl -0x54(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x1ef0c
movb $0x0, -0x181(%rbp)
jmp 0x1ef0e
testb $0x1, -0x181(%rbp)
je 0x1ef68
movq -0x210(%rbp), %rax
movq 0x150(%rax), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movslq -0x58(%rbp), %rsi
callq 0x347f0
movq %rax, -0x2a0(%rbp)
jmp 0x1ef3f
movq -0x2a0(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
callq 0x34830
jmp 0x1ef54
movl -0x58(%rbp), %eax
movl %eax, -0x188(%rbp)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x1efb7
movq -0x210(%rbp), %rax
movq 0x148(%rax), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movslq -0x54(%rbp), %rsi
callq 0x347f0
movq %rax, -0x2a8(%rbp)
jmp 0x1ef90
movq -0x2a8(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
callq 0x34830
jmp 0x1efa5
movl -0x54(%rbp), %eax
movl %eax, -0x188(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
testb $0x1, -0x59(%rbp)
je 0x1f20f
movl -0xb8(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
subl -0x24(%rbp), %eax
cmpl $0x2, %eax
jl 0x1f20d
movq -0x210(%rbp), %rdi
movl -0x28(%rbp), %eax
movl -0x24(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x18c(%rbp)
addq $0x118, %rdi            # imm = 0x118
leaq -0xc(%rbp), %rsi
callq 0x32770
movq %rax, -0x2b0(%rbp)
jmp 0x1f007
movq -0x2b0(%rbp), %rsi
movslq -0x24(%rbp), %rdx
movslq -0x18c(%rbp), %rcx
leaq -0x1b0(%rbp), %rdi
callq 0x6460
jmp 0x1f027
movl -0x18(%rbp), %eax
movl -0x1c(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x1b8(%rbp)
movl -0x14(%rbp), %eax
movl -0x20(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x1bc(%rbp)
leaq -0x1b8(%rbp), %rdi
leaq -0x1bc(%rbp), %rsi
callq 0x32a80
movq %rax, -0x2b8(%rbp)
jmp 0x1f05f
movq -0x2b8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x1b4(%rbp)
movb $0x0, -0x1bd(%rbp)
cmpl $0x0, -0x1b4(%rbp)
jne 0x1f0a1
movb $0x1, -0x1bd(%rbp)
jmp 0x1f192
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
jmp 0x1f1fc
movl -0x18c(%rbp), %ecx
movl -0x1b4(%rbp), %edx
shll %edx
movb $0x0, -0x1e1(%rbp)
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x2b9(%rbp)
jle 0x1f12d
movb $0x1, %al
cmpl $0x3e8, -0x18c(%rbp)    # imm = 0x3E8
movb %al, -0x2ba(%rbp)
jg 0x1f121
leaq -0x1e0(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x61b0
jmp 0x1f0eb
movq -0x210(%rbp), %rdi
movb $0x1, -0x1e1(%rbp)
movl -0x1b4(%rbp), %edx
leaq -0x1e0(%rbp), %rsi
callq 0x1f3a0
movb %al, -0x2bb(%rbp)
jmp 0x1f113
movb -0x2bb(%rbp), %al
xorb $-0x1, %al
movb %al, -0x2ba(%rbp)
movb -0x2ba(%rbp), %al
movb %al, -0x2b9(%rbp)
movb -0x2b9(%rbp), %al
movb %al, -0x2bc(%rbp)
testb $0x1, -0x1e1(%rbp)
jne 0x1f144
jmp 0x1f150
leaq -0x1e0(%rbp), %rdi
callq 0x6828
movb -0x2bc(%rbp), %al
testb $0x1, %al
jne 0x1f15c
jmp 0x1f190
movb $0x1, -0x1bd(%rbp)
jmp 0x1f190
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
testb $0x1, -0x1e1(%rbp)
jne 0x1f182
jmp 0x1f18e
leaq -0x1e0(%rbp), %rdi
callq 0x6828
jmp 0x1f1fc
jmp 0x1f192
testb $0x1, -0x1bd(%rbp)
je 0x1f1ee
movq -0x210(%rbp), %rax
movq 0x158(%rax), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq -0x180(%rbp), %rsi
callq 0x363c0
jmp 0x1f1bf
leaq -0x180(%rbp), %rdi
callq 0x36430
movl $0x0, -0x18(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x24(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x6828
jmp 0x1f20d
leaq -0x1b0(%rbp), %rdi
callq 0x6828
jmp 0x1f353
jmp 0x1f20f
movl -0xb8(%rbp), %eax
movl %eax, -0x2cc(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x6630
movq %rax, %rcx
movl -0x2cc(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x1e8(%rbp)
leaq -0x24(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x32a80
movq %rax, -0x2c8(%rbp)
jmp 0x1f251
movq -0x2c8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x24(%rbp)
movl -0xc(%rbp), %esi
movl -0x188(%rbp), %edx
movb -0x181(%rbp), %al
notb %al
movzbl %al, %ecx
andl $0x1, %ecx
leaq -0x1f4(%rbp), %rdi
callq 0x36450
jmp 0x1f282
leaq -0x180(%rbp), %rdi
leaq -0x1f4(%rbp), %rsi
callq 0x36480
jmp 0x1f297
testb $0x1, -0x59(%rbp)
jne 0x1f2a1
movb $0x1, -0x59(%rbp)
leaq -0xb8(%rbp), %rdi
addq $0x8, %rdi
callq 0x6630
movl %eax, -0x1f8(%rbp)
movl $0x0, -0x1fc(%rbp)
testb $0x1, -0x6c(%rbp)
je 0x1f2d1
movl $0x1, -0x1fc(%rbp)
movl -0x70(%rbp), %ecx
movslq -0x1fc(%rbp), %rax
addl -0x18(%rbp,%rax,4), %ecx
movl %ecx, -0x18(%rbp,%rax,4)
movl -0x74(%rbp), %ecx
movslq -0x1fc(%rbp), %rax
addl -0x20(%rbp,%rax,4), %ecx
movl %ecx, -0x20(%rbp,%rax,4)
jmp 0x1ecbb
leaq -0x180(%rbp), %rdi
callq 0x33a90
cmpq $0x0, %rax
jbe 0x1f332
movq -0x210(%rbp), %rax
movq 0x158(%rax), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq -0x180(%rbp), %rsi
callq 0x363c0
jmp 0x1f330
jmp 0x1f332
leaq -0x180(%rbp), %rdi
callq 0x348a0
leaq -0xb8(%rbp), %rdi
callq 0x32c60
addq $0x2f0, %rsp            # imm = 0x2F0
popq %rbp
retq
leaq -0x180(%rbp), %rdi
callq 0x348a0
leaq -0xb8(%rbp), %rdi
callq 0x32c60
movq -0xe8(%rbp), %rdi
callq 0x6750
nopw (%rax,%rax)