// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movsd %xmm0, 0x20(%rsp)
movq %rcx, (%rsp)
movq %rsi, %r14
movq %rdi, %r15
movq 0x8(%rdx), %rax
cmpq (%rdx), %rax
je 0x162e9
movq %r15, %rdi
callq 0x163f1
movq %rax, %rbx
movq %r14, %rdi
callq 0x163f1
movq %rax, %r12
movq %rbx, 0x18(%rsp)
movq 0x18(%rbx), %rbx
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movq %rdx, %rcx
subq %rax, %rcx
shrq $0x3, %rcx
testl %ecx, %ecx
jle 0x162c8
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, 0x38(%rsp)
xorl %ebp, %ebp
movq %r12, 0x30(%rsp)
movq %rbx, 0x28(%rsp)
movq 0x38(%rsp), %rcx
movq (%rcx,%rbp,8), %r15
movq (%r15), %r14
movq 0x8(%r15), %r13
cmpq %r13, %r14
je 0x1605c
movq %r13, %rax
subq %r14, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %r13, %rsi
callq 0x1c8fb
movq %r14, %rdi
movq %r13, %rsi
callq 0x1c9e4
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbp, %rdx
jbe 0x163d8
movq 0x18(%r12), %r14
movq (%rax,%rbp,8), %rsi
movq %r14, %rdi
callq 0x26ed0
movq 0x8(%r14), %rcx
subq (%r14), %rcx
shrq $0x3, %rcx
cmpl %ecx, %eax
movq %rbp, 0x40(%rsp)
jge 0x1627b
movq (%r12), %rcx
cltq
movq (%rcx,%rax,8), %r13
movq (%r13), %r14
movq 0x8(%r13), %rbp
cmpq %rbp, %r14
je 0x160d9
movq %rbp, %rax
subq %r14, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x1c8fb
movq %r14, %rdi
movq %rbp, %rsi
callq 0x1c9e4
movq (%r15), %r12
movq 0x8(%r15), %r14
subq %r12, %r14
sarq $0x3, %r14
testl %r14d, %r14d
jle 0x16299
xorl %ebp, %ebp
cmpq %rbp, %r14
jbe 0x163b4
movq (%r12,%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 0x167a8
movq (%r13), %rdi
movq 0x8(%r13), %r8
subq %rdi, %r8
movq %r8, %rdx
sarq $0x3, %rdx
testl %eax, %eax
jle 0x1616e
movl %eax, %esi
cmpq %rsi, %rdx
jbe 0x163ca
movq (%r15), %r9
movq 0x8(%r15), %rcx
subq %r9, %rcx
sarq $0x3, %rcx
cmpq %rbp, %rcx
jbe 0x163a5
movq (%rdi,%rsi,8), %rcx
movl 0x20(%rcx), %ecx
movq (%r9,%rbp,8), %rsi
cmpl 0xc(%rsi), %ecx
jle 0x1616e
leal -0x1(%rax), %ecx
cmpl $0x1, %eax
movl %ecx, %eax
jg 0x16130
xorl %eax, %eax
cmpl %edx, %eax
jge 0x161fe
movslq %eax, %rsi
shrq $0x3, %r8
movslq %r8d, %rcx
xorl %eax, %eax
movq %rsi, %r8
cmpq %r8, %rdx
jbe 0x163c3
movq (%r15), %r12
movq 0x8(%r15), %r14
subq %r12, %r14
sarq $0x3, %r14
cmpq %rbp, %r14
jbe 0x163b4
movq (%rdi,%r8,8), %r9
movl 0xc(%r9), %ebx
movq (%r12,%rbp,8), %r10
movl 0x20(%r10), %r11d
cmpl %r11d, %ebx
jge 0x161f5
cmpl %ebx, 0xc(%r10)
movq %r9, %rbx
cmovgq %r10, %rbx
cmpl 0x20(%r9), %r11d
cmovlq %r10, %r9
movl 0x20(%r9), %r9d
movl 0xc(%rbx), %r10d
movl %r9d, %r11d
subl %r10d, %r11d
cmpl %r11d, %eax
leal 0x1(%r11), %r11d
cmovgl %eax, %r11d
cmpl %r10d, %r9d
cmovgl %r11d, %eax
incq %r8
cmpq %r8, %rcx
jne 0x16185
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
jmp 0x16210
movq (%r15), %r12
movq 0x8(%r15), %r14
subq %r12, %r14
sarq $0x3, %r14
xorpd %xmm0, %xmm0
cmpq %rbp, %r14
jbe 0x163b4
movq (%r12,%rbp,8), %rax
movl 0x20(%rax), %ecx
subl 0xc(%rax), %ecx
incl %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
divsd 0x20(%rsp), %xmm0
callq 0x22c0
cvttsd2si %xmm0, %ecx
movq (%rsp), %rdx
movq (%rdx), %rax
movq 0x8(%rdx), %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 0x163ca
incl (%rax,%rsi,4)
incq %rbp
movslq %r14d, %rax
cmpq %rax, %rbp
jl 0x160f2
jmp 0x16299
movq (%rsp), %rcx
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x163e4
movq 0x8(%rbx), %rcx
subq (%rbx), %rcx
shrq $0x3, %rcx
addl %ecx, (%rax)
movq 0x40(%rsp), %rbp
incq %rbp
movq 0x28(%rsp), %rbx
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movq %rdx, %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rbp
movq 0x30(%rsp), %r12
jl 0x16013
movq 0x18(%rsp), %rdi
callq 0x16877
movq %r12, %rdi
callq 0x16877
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r12
movq 0x22ced(%rip), %rax     # 0x38fe0
movq (%rax), %rcx
leaq 0x1760f(%rip), %rdi     # 0x2d90c
movl $0x29, %esi
movl $0x1, %edx
callq 0x2340
leaq 0x8(%rsp), %r13
movq $0x0, (%r13)
leaq 0x14(%rsp), %rbp
movq (%rsp), %rdi
movl $0x0, 0x14(%rsp)
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x16344
movl $0x0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x16350
movq %rbp, %rdx
callq 0xae72
movq (%rsp), %rdi
movq 0x8(%r12), %rsi
cmpq 0x10(%r12), %rsi
je 0x16371
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%r12)
jmp 0x16386
movq %r12, %rdi
movq %r13, %rdx
callq 0xb60c
movq (%rsp), %rdi
movsd 0x8(%rsp), %xmm0
addsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x13c6e(%rip), %xmm1    # 0x2a008
ucomisd %xmm0, %xmm1
ja 0x16322
jmp 0x15fc2
leaq 0x16524(%rip), %rdi     # 0x2c8d0
movq %rbp, %rsi
movq %rcx, %rdx
jmp 0x163d1
leaq 0x16515(%rip), %rdi     # 0x2c8d0
movq %rbp, %rsi
movq %r14, %rdx
jmp 0x163d1
cmpq %rsi, %rdx
cmovaq %rdx, %rsi
leaq 0x164ff(%rip), %rdi     # 0x2c8d0
xorl %eax, %eax
callq 0x2270
leaq 0x164f1(%rip), %rdi     # 0x2c8d0
movq %rbp, %rsi
jmp 0x163d1
leaq 0x164e5(%rip), %rdi     # 0x2c8d0
xorl %esi, %esi
xorl %edx, %edx
jmp 0x163d1