// File: /workspace/github_cpp_makefile_star2_O1/gersteinlab[P]MUSIC/src/ms_annot_region_tools.cpp
// Function: get_overlap_statistics(std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, std::vector<double, std::allocator<double>>*, std::vector<int, std::allocator<int>>*, double)
void get_overlap_statistics(vector<t_annot_region*>* regs1, vector<t_annot_region*>* regs2, 
							vector<double>* bases, vector<int>* stat_counts, double sim_step)
{
	if(bases->size() == 0)
	{
		fprintf(stderr, "Setting up overlapping bases and counts.\n");
		double cur_sim = 0.0;
		while(cur_sim < 1.0)
		{
			stat_counts->push_back(0);
			bases->push_back(cur_sim);
			cur_sim += sim_step;
		} // sim_step loop.
	}

	t_sorted_annot_region_lists* regs1_res = restructure_annot_regions(regs1);
	t_sorted_annot_region_lists* regs2_res = restructure_annot_regions(regs2);

	for(int i_chr = 0; i_chr < (int)regs1_res->chr_ids->size(); i_chr++)
	{
		vector<t_annot_region*>* cur_reg1_regs = regs1_res->regions_per_chrom[i_chr];
		sort(cur_reg1_regs->begin(), cur_reg1_regs->end());

		// Compare the TARs that are overlapping.
		int i_chr2 = t_string::get_i_str(regs2_res->chr_ids, regs1_res->chr_ids->at(i_chr));

		if(i_chr2 < (int)regs2_res->chr_ids->size())
		{
			vector<t_annot_region*>* cur_reg2_regs = regs2_res->regions_per_chrom[i_chr2];
			sort(cur_reg2_regs->begin(), cur_reg2_regs->end());

			for(int i_reg = 0; i_reg < (int)cur_reg1_regs->size(); i_reg++)
			{
				// Find the region in the region list via binary search.
				int i_posn = locate_posn_region_per_region_starts(cur_reg1_regs->at(i_reg)->start, cur_reg2_regs, 0, cur_reg2_regs->size()-1);

				while(i_posn > 0 && 
					cur_reg2_regs->at(i_posn)->end > cur_reg1_regs->at(i_reg)->start)
				{
					i_posn--;
				}

				int max_l_overlap = 0;
				while(i_posn < (int)cur_reg2_regs->size() &&
					cur_reg2_regs->at(i_posn)->start < cur_reg1_regs->at(i_reg)->end)
				{
					int ol_start = MAX(cur_reg1_regs->at(i_reg)->start, cur_reg2_regs->at(i_posn)->start);
					int ol_end = MIN(cur_reg1_regs->at(i_reg)->end, cur_reg2_regs->at(i_posn)->end);

					if(ol_end > ol_start)
					{
						int l_cur_ol = (ol_end-ol_start+1);
						if(max_l_overlap < l_cur_ol)
						{
							max_l_overlap = l_cur_ol;
						}
					}

					i_posn++;
				} // i_posn loop.

				double ol_fraction = (double)max_l_overlap / (cur_reg1_regs->at(i_reg)->end - cur_reg1_regs->at(i_reg)->start + 1);

				// Get the id for the current overlap fraction.
				int i_ol_fraction = (int)floor((double)ol_fraction / (double)sim_step);

				if(i_ol_fraction == (int)stat_counts->size())
				{
					i_ol_fraction--;
				}

				// Update the count.
				stat_counts->at(i_ol_fraction) = stat_counts->at(i_ol_fraction)+1;
			} // i_reg loop.
		}
		else
		{
			// All of the regions in this chromosome have 0% overlap.
			stat_counts->at(0) = stat_counts->at(0) + (int)regs1_res->chr_ids->size();
		}
	} // i_chr loop.

	// Free memory.
	delete_restructured_annot_regions(regs1_res);
	delete_restructured_annot_regions(regs2_res);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movsd %xmm0, 0x30(%rsp)
movq %rcx, %rbx
movq %rsi, %r15
movq %rdi, %r14
movq 0x8(%rdx), %rax
cmpq (%rdx), %rax
je 0x16643
movq %r14, %rdi
callq 0x16742
movq %rax, %r14
movq %r15, %rdi
callq 0x16742
movq %rax, 0x18(%rsp)
movq 0x18(%r14), %rax
movq 0x8(%rax), %rcx
subq (%rax), %rcx
shrq $0x3, %rcx
testl %ecx, %ecx
jle 0x16622
xorl %edx, %edx
movq %r14, 0x20(%rsp)
movq (%r14), %rax
movq %rdx, 0x28(%rsp)
movq (%rax,%rdx,8), %r14
movq (%r14), %r13
movq 0x8(%r14), %rbp
cmpq %rbp, %r13
je 0x16377
movq %rbp, %rax
subq %r13, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x1cd31
movq %r13, %rdi
movq %rbp, %rsi
callq 0x1ce1a
movq 0x20(%rsp), %r15
movq 0x18(%r15), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movq 0x28(%rsp), %rsi
cmpq %rsi, %rdx
jbe 0x1671b
movq 0x18(%rsp), %r12
movq 0x18(%r12), %rdi
movq (%rax,%rsi,8), %rsi
callq 0x274fc
movq 0x18(%r12), %rcx
movq 0x8(%rcx), %rdx
subq (%rcx), %rdx
shrq $0x3, %rdx
cmpl %edx, %eax
jge 0x165dc
movq (%r12), %rcx
cltq
movq (%rcx,%rax,8), %r13
movq (%r13), %rbp
movq 0x8(%r13), %r15
cmpq %r15, %rbp
je 0x1640b
movq %r15, %rax
subq %rbp, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x1cd31
movq %rbp, %rdi
movq %r15, %rsi
callq 0x1ce1a
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x165fa
xorl %ebp, %ebp
cmpq %rbp, %rdx
jbe 0x16729
movq (%rax,%rbp,8), %rax
movl 0xc(%rax), %edi
movq 0x8(%r13), %rcx
subq (%r13), %rcx
shrq $0x3, %rcx
decl %ecx
movq %r13, %rsi
xorl %edx, %edx
callq 0x16b4d
testl %eax, %eax
jle 0x1649c
movq (%r13), %rdi
movq 0x8(%r13), %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
movl %eax, %esi
cmpq %rsi, %rdx
jbe 0x1671b
movq (%r14), %r8
movq 0x8(%r14), %rcx
subq %r8, %rcx
sarq $0x3, %rcx
cmpq %rbp, %rcx
jbe 0x166f6
movq (%rdi,%rsi,8), %rcx
movl 0x20(%rcx), %ecx
movq (%r8,%rbp,8), %rsi
cmpl 0xc(%rsi), %ecx
jle 0x1649c
leal -0x1(%rax), %ecx
cmpl $0x1, %eax
movl %ecx, %eax
jg 0x1645e
xorl %eax, %eax
movq (%r13), %rcx
movq 0x8(%r13), %rdi
subq %rcx, %rdi
movq %rdi, %rdx
sarq $0x3, %rdx
xorpd %xmm0, %xmm0
cmpl %edx, %eax
jge 0x16552
movslq %eax, %rsi
shrq $0x3, %rdi
movslq %edi, %rdi
leaq (%rcx,%rsi,8), %r8
xorl %ecx, %ecx
movq %rsi, %r9
cmpq %r9, %rdx
jbe 0x16714
movq (%r14), %r10
movq 0x8(%r14), %rax
subq %r10, %rax
sarq $0x3, %rax
cmpq %rbp, %rax
jbe 0x16705
movq (%r8), %rax
movl 0xc(%rax), %r15d
movq (%r10,%rbp,8), %r12
movl 0x20(%r12), %r11d
cmpl %r11d, %r15d
jge 0x1654b
leaq (%r10,%rbp,8), %r10
cmpl %r15d, 0xc(%r12)
movq %r8, %r15
cmovgq %r10, %r15
cmpl 0x20(%rax), %r11d
movq (%r15), %rax
cmovgeq %r8, %r10
movq (%r10), %r10
movl 0x20(%r10), %r10d
movl 0xc(%rax), %eax
movl %r10d, %r11d
subl %eax, %r11d
cmpl %r11d, %ecx
leal 0x1(%r11), %r11d
cmovgl %ecx, %r11d
cmpl %eax, %r10d
cmovgl %r11d, %ecx
incq %r9
addq $0x8, %r8
cmpq %r9, %rdi
jne 0x164cd
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbp, %rdx
jbe 0x16729
movq (%rax,%rbp,8), %rax
movl 0x20(%rax), %ecx
subl 0xc(%rax), %ecx
incl %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
divsd 0x30(%rsp), %xmm0
callq 0x22c0
cvttsd2si %xmm0, %ecx
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
xorl %esi, %esi
cmpl %edx, %ecx
sete %sil
subl %esi, %ecx
movslq %ecx, %rsi
cmpq %rsi, %rdx
jbe 0x1671b
incl (%rax,%rsi,4)
incq %rbp
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rbp
jl 0x16423
jmp 0x165fa
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x16735
movq 0x18(%r15), %rcx
movq 0x8(%rcx), %rdx
subq (%rcx), %rdx
shrq $0x3, %rdx
addl %edx, (%rax)
movq 0x20(%rsp), %r14
movq 0x18(%r14), %rax
movq 0x8(%rax), %rcx
subq (%rax), %rcx
movq 0x28(%rsp), %rdx
incq %rdx
shrq $0x3, %rcx
movslq %ecx, %rax
cmpq %rax, %rdx
jl 0x16332
movq %r14, %rdi
callq 0x16c1c
movq 0x18(%rsp), %rdi
callq 0x16c1c
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r12
movq 0x23993(%rip), %rax     # 0x39fe0
movq (%rax), %rcx
leaq 0x182b5(%rip), %rdi     # 0x2e90c
movl $0x29, %esi
movl $0x1, %edx
callq 0x2340
leaq 0x8(%rsp), %r13
movq $0x0, (%r13)
leaq 0x14(%rsp), %rbp
movl $0x0, 0x14(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x1669a
movl $0x0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x166a5
movq %rbx, %rdi
movq %rbp, %rdx
callq 0xb048
movq 0x8(%r12), %rsi
cmpq 0x10(%r12), %rsi
je 0x166c6
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%r12)
jmp 0x166d1
movq %r12, %rdi
movq %r13, %rdx
callq 0xb836
movsd 0x8(%rsp), %xmm0
addsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x1491d(%rip), %xmm1    # 0x2b008
ucomisd %xmm0, %xmm1
ja 0x16678
jmp 0x162fc
leaq 0x171d9(%rip), %rdi     # 0x2d8d6
movq %rbp, %rsi
movq %rcx, %rdx
jmp 0x16722
leaq 0x171ca(%rip), %rdi     # 0x2d8d6
movq %rbp, %rsi
movq %rax, %rdx
jmp 0x16722
cmpq %rsi, %rdx
cmovaq %rdx, %rsi
leaq 0x171b4(%rip), %rdi     # 0x2d8d6
xorl %eax, %eax
callq 0x2270
leaq 0x171a6(%rip), %rdi     # 0x2d8d6
movq %rbp, %rsi
jmp 0x16722
leaq 0x1719a(%rip), %rdi     # 0x2d8d6
xorl %esi, %esi
xorl %edx, %edx
jmp 0x16722