// File: 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:
subq $0x118, %rsp            # imm = 0x118
movq %rdi, 0x110(%rsp)
movq %rsi, 0x108(%rsp)
movq %rdx, 0x100(%rsp)
movq %rcx, 0xf8(%rsp)
movsd %xmm0, 0xf0(%rsp)
movq 0x100(%rsp), %rdi
callq 0x11c40
cmpq $0x0, %rax
jne 0x28c02
movq 0x3c462(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x2d144(%rip), %rsi     # 0x55ccc
movb $0x0, %al
callq 0x2250
xorps %xmm0, %xmm0
movsd %xmm0, 0xe8(%rsp)
movsd 0x29475(%rip), %xmm0    # 0x52018
ucomisd 0xe8(%rsp), %xmm0
jbe 0x28c00
movq 0xf8(%rsp), %rdi
movl $0x0, 0xe4(%rsp)
leaq 0xe4(%rsp), %rsi
callq 0xc420
movq 0x100(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0xc850
movsd 0xf0(%rsp), %xmm0
addsd 0xe8(%rsp), %xmm0
movsd %xmm0, 0xe8(%rsp)
jmp 0x28b9b
jmp 0x28c02
movq 0x110(%rsp), %rdi
callq 0x29200
movq %rax, 0xd8(%rsp)
movq 0x108(%rsp), %rdi
callq 0x29200
movq %rax, 0xd0(%rsp)
movl $0x0, 0xcc(%rsp)
movl 0xcc(%rsp), %eax
movl %eax, 0x68(%rsp)
movq 0xd8(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x68(%rsp), %eax
cmpl %ecx, %eax
jge 0x291d1
movq 0xd8(%rsp), %rax
movq (%rax), %rax
movslq 0xcc(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rdi
callq 0xc7d0
movq %rax, 0xb8(%rsp)
movq 0xc0(%rsp), %rdi
callq 0xc760
movq %rax, 0xb0(%rsp)
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x31d50
movq 0xd0(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x58(%rsp)
movq 0xd8(%rsp), %rax
movq 0x18(%rax), %rdi
movslq 0xcc(%rsp), %rsi
callq 0xc240
movq 0x58(%rsp), %rdi
movq (%rax), %rsi
callq 0x47cf0
movl %eax, 0xac(%rsp)
movl 0xac(%rsp), %eax
movl %eax, 0x64(%rsp)
movq 0xd0(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x64(%rsp), %eax
cmpl %ecx, %eax
jge 0x2916d
movq 0xd0(%rsp), %rax
movq (%rax), %rax
movslq 0xac(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rdi
callq 0xc7d0
movq %rax, 0x98(%rsp)
movq 0xa0(%rsp), %rdi
callq 0xc760
movq %rax, 0x90(%rsp)
movq 0x98(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x31d50
movl $0x0, 0x8c(%rsp)
movl 0x8c(%rsp), %eax
movl %eax, 0x54(%rsp)
movq 0xc0(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0x54(%rsp), %eax
cmpl %ecx, %eax
jge 0x2916b
movq 0xc0(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x44(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0xa0(%rsp), %rdi
callq 0xc680
movl 0x44(%rsp), %edi
movq 0x48(%rsp), %rsi
subq $0x1, %rax
movl %eax, %ecx
xorl %edx, %edx
callq 0x297a0
movl %eax, 0x88(%rsp)
xorl %eax, %eax
cmpl $0x0, 0x88(%rsp)
movb %al, 0x43(%rsp)
jle 0x28e67
movq 0xa0(%rsp), %rdi
movslq 0x88(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x3c(%rsp)
movq 0xc0(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x3c(%rsp), %eax
movq (%rcx), %rcx
cmpl 0xc(%rcx), %eax
setg %al
movb %al, 0x43(%rsp)
movb 0x43(%rsp), %al
testb $0x1, %al
jne 0x28e71
jmp 0x28e84
movl 0x88(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x88(%rsp)
jmp 0x28e0f
movl $0x0, 0x84(%rsp)
movl 0x88(%rsp), %eax
movl %eax, 0x34(%rsp)
movq 0xa0(%rsp), %rdi
callq 0xc680
movl 0x34(%rsp), %ecx
movl %eax, %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, 0x3b(%rsp)
jge 0x28eff
movq 0xa0(%rsp), %rdi
movslq 0x88(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x30(%rsp)
movq 0xc0(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x30(%rsp), %eax
movq (%rcx), %rcx
cmpl 0x20(%rcx), %eax
setl %al
movb %al, 0x3b(%rsp)
movb 0x3b(%rsp), %al
testb $0x1, %al
jne 0x28f0c
jmp 0x29074
movq 0xc0(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x2c(%rsp)
movq 0xa0(%rsp), %rdi
movslq 0x88(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x2c(%rsp), %eax
movq (%rcx), %rcx
cmpl 0xc(%rcx), %eax
jle 0x28f70
movq 0xc0(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x28(%rsp)
jmp 0x28f8f
movq 0xa0(%rsp), %rdi
movslq 0x88(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
movl %eax, 0x80(%rsp)
movq 0xc0(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x24(%rsp)
movq 0xa0(%rsp), %rdi
movslq 0x88(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x24(%rsp), %eax
movq (%rcx), %rcx
cmpl 0x20(%rcx), %eax
jge 0x28ffe
movq 0xc0(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x20(%rsp)
jmp 0x2901d
movq 0xa0(%rsp), %rdi
movslq 0x88(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x20(%rsp)
movl 0x20(%rsp), %eax
movl %eax, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
cmpl 0x80(%rsp), %eax
jle 0x2905e
movl 0x7c(%rsp), %eax
subl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
movl 0x84(%rsp), %eax
cmpl 0x78(%rsp), %eax
jge 0x2905c
movl 0x78(%rsp), %eax
movl %eax, 0x84(%rsp)
jmp 0x2905e
movl 0x88(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x88(%rsp)
jmp 0x28e8f
cvtsi2sdl 0x84(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0xc0(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x18(%rsp)
movq 0xc0(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0xc800
movsd 0x10(%rsp), %xmm0
movq %rax, %rcx
movl 0x18(%rsp), %eax
movq (%rcx), %rcx
subl 0xc(%rcx), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x70(%rsp), %xmm0
divsd 0xf0(%rsp), %xmm0
callq 0x22e0
cvttsd2si %xmm0, %eax
movl %eax, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0xf8(%rsp), %rdi
callq 0xc2a0
movq %rax, %rcx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jne 0x29122
movl 0x6c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x6c(%rsp)
movq 0xf8(%rsp), %rdi
movslq 0x6c(%rsp), %rsi
callq 0xc2c0
movl (%rax), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
movq 0xf8(%rsp), %rdi
movslq 0x6c(%rsp), %rsi
callq 0xc2c0
movl 0xc(%rsp), %ecx
movl %ecx, (%rax)
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x28d92
jmp 0x291b9
movq 0xf8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xc2c0
movl (%rax), %eax
movl %eax, 0x4(%rsp)
movq 0xd8(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x4(%rsp), %eax
addl %ecx, %eax
movl %eax, 0x8(%rsp)
movq 0xf8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xc2c0
movl 0x8(%rsp), %ecx
movl %ecx, (%rax)
jmp 0x291bb
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x28c37
movq 0xd8(%rsp), %rdi
callq 0x29990
movq 0xd0(%rsp), %rdi
callq 0x29990
addq $0x118, %rsp            # imm = 0x118
retq
nopw %cs:(%rax,%rax)