// File: /workspace/github_cpp_makefile_star2_O1/gersteinlab[P]MUSIC/src/ms_annot_region_tools.cpp
// Function: intersect_annot_regions(std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, bool)
vector<t_annot_region*>* intersect_annot_regions(vector<t_annot_region*>* annot_regions1,
														vector<t_annot_region*>* annot_regions2,
														bool find_all_overlaps)
{
	vector<t_annot_region*>* intersected_regions = new vector<t_annot_region*>();

	// Do not merge the regions any more.
	sort_set_sorting_info(annot_regions2, sort_regions); 

	// Restructure each list of regions.
	t_sorted_annot_region_lists* restructured_regs1 = restructure_annot_regions(annot_regions1);
	t_sorted_annot_region_lists* restructured_regs2 = restructure_annot_regions(annot_regions2);

	for(int i_chr1 = 0; i_chr1 < (int)restructured_regs1->chr_ids->size(); i_chr1++)
	{
		int i_chr2 = t_string::get_i_str(restructured_regs2->chr_ids, restructured_regs1->chr_ids->at(i_chr1));

		if(i_chr2 < (int)restructured_regs2->chr_ids->size())
		{
			vector<t_annot_region*>* cur_chr_regs1 = restructured_regs1->regions_per_chrom[i_chr1];
			vector<t_annot_region*>* cur_chr_regs2 = restructured_regs2->regions_per_chrom[i_chr2];

			for(int i_reg1 = 0; i_reg1 < (int)cur_chr_regs1->size(); i_reg1++)
			{
				int i_leftmost_reg = locate_posn_region_per_region_starts(cur_chr_regs1->at(i_reg1)->start, cur_chr_regs2, 0, (int)cur_chr_regs2->size()-1);

				// Go back till the cumulative end for the reg2 is to the left of reg1.
				while(i_leftmost_reg > 0 && 
					cur_chr_regs2->at(i_leftmost_reg)->sort_info->cumulative_sorted_end > cur_chr_regs1->at(i_reg1)->start)
				{
					i_leftmost_reg--;
				} // i_leftmost_reg loop.

				// Check if there is any overlap.
				//bool region_overlaps = false;
				int ol_start = 0;
				int ol_end = 0;
				while(i_leftmost_reg < (int)cur_chr_regs2->size() &&
					cur_chr_regs2->at(i_leftmost_reg)->start <= cur_chr_regs1->at(i_reg1)->end)
				{
					ol_start = MAX(cur_chr_regs2->at(i_leftmost_reg)->start, cur_chr_regs1->at(i_reg1)->start);
					ol_end = MIN(cur_chr_regs2->at(i_leftmost_reg)->end, cur_chr_regs1->at(i_reg1)->end);

					if(ol_end >= ol_start)
					{
						// There is overlap: Set the start and end to ol_start and ol_end.
						t_annot_region* new_intersecting_region = duplicate_region(cur_chr_regs1->at(i_reg1));
						new_intersecting_region->start = ol_start;
						new_intersecting_region->end = ol_end;

						// Update the intersection info.
						t_intersect_info* cur_intersection_info = new t_intersect_info();
						cur_intersection_info->src_reg = cur_chr_regs1->at(i_reg1);
						cur_intersection_info->dest_reg = cur_chr_regs2->at(i_leftmost_reg);
						cur_intersection_info->l_overlap = ol_end - ol_start + 1;
						new_intersecting_region->data = cur_intersection_info;

						intersected_regions->push_back(new_intersecting_region);

						// Found the first overlap, if looking for just one overlap, return one overlap, otherwise continue since there can be more overlaps.
						if(!find_all_overlaps)
						{
							break;
						}
					}
					i_leftmost_reg++;
				}

				//// Does this region overlap?
				//if(region_overlaps)
				//{
				//	t_annot_region* new_intersecting_region = duplicate_region(cur_chr_regs1->at(i_reg1));
				//	new_intersecting_region->start = ol_start;
				//	new_intersecting_region->end = ol_end;

				//	// Update the intersection info.
				//	t_intersect_info* cur_intersection_info = new t_intersect_info();
				//	cur_intersection_info->src_reg = cur_chr_regs1->at(i_reg1);
				//	cur_intersection_info->dest_reg = cur_chr_regs2->at(i_leftmost_reg);
				//	new_intersecting_region->data = cur_intersection_info;

				//	intersected_regions->push_back(new_intersecting_region);
				//}
			} // i_reg1 loop.
		} // i_chr2 check.
		else
		{
			// This chromosome does not exist in region2 list, cannot overlap two region lists for this chromosome.
		}
	} // i_chr1 loop.

	// Delete the sorting information for regions2.
	delete_sorting_information(annot_regions2);

	delete_restructured_annot_regions(restructured_regs1);
	delete_restructured_annot_regions(restructured_regs2);

	return(intersected_regions);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, 0x1c(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
movl $0x18, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x28(%rsp)
movq $0x0, 0x10(%rax)
leaq -0x219e(%rip), %rsi     # 0x17291
movq %rbx, %rdi
callq 0x15f96
movq %r14, %rdi
callq 0x16742
movq %rax, %r14
movq %rbx, 0x38(%rsp)
movq %rbx, %rdi
callq 0x16742
movq %rax, 0x8(%rsp)
movq %r14, 0x10(%rsp)
movq 0x18(%r14), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x1973f
xorl %esi, %esi
cmpq %rsi, %rdx
jbe 0x19771
movq 0x8(%rsp), %rbx
movq 0x18(%rbx), %rdi
movq %rsi, 0x20(%rsp)
movq (%rax,%rsi,8), %rsi
callq 0x274fc
movq 0x18(%rbx), %rcx
movq 0x8(%rcx), %rdx
subq (%rcx), %rdx
shrq $0x3, %rdx
cmpl %edx, %eax
jge 0x1970d
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq 0x20(%rsp), %rdx
movq (%rcx,%rdx,8), %r10
movq (%r10), %rcx
movq 0x8(%r10), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x1970d
movq 0x8(%rsp), %rsi
movq (%rsi), %rsi
cltq
movq (%rsi,%rax,8), %r11
xorl %r15d, %r15d
movq %r10, (%rsp)
movq %r11, 0x30(%rsp)
cmpq %r15, %rdx
jbe 0x19795
movq (%rcx,%r15,8), %rax
movl 0xc(%rax), %edi
movq 0x8(%r11), %rcx
subq (%r11), %rcx
shrq $0x3, %rcx
decl %ecx
movq %r11, %rsi
xorl %edx, %edx
movq %r11, %rbx
callq 0x16b4d
testl %eax, %eax
jle 0x19574
movq (%rbx), %rdi
movq 0x8(%rbx), %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
movq (%rsp), %r10
movq %rbx, %r11
movl %eax, %esi
cmpq %rsi, %rdx
jbe 0x19771
movq (%r10), %r8
movq 0x8(%r10), %rcx
subq %r8, %rcx
sarq $0x3, %rcx
cmpq %r15, %rcx
jbe 0x1977a
movq (%rdi,%rsi,8), %rcx
movq 0x38(%rcx), %rcx
movl 0x4(%rcx), %ecx
movq (%r8,%r15,8), %rsi
cmpl 0xc(%rsi), %ecx
jle 0x1957b
leal -0x1(%rax), %ecx
cmpl $0x1, %eax
movl %ecx, %eax
jg 0x19530
xorl %eax, %eax
jmp 0x1957b
movq (%rsp), %r10
movq %rbx, %r11
movq (%r11), %rcx
movq 0x8(%r11), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpl %edx, %eax
jge 0x196ea
movslq %eax, %r14
cmpq %r14, %rdx
jbe 0x19789
movq (%r10), %rsi
movq 0x8(%r10), %rdx
movq %rdx, %rax
subq %rsi, %rax
sarq $0x3, %rax
cmpq %r15, %rax
jbe 0x197a1
movq (%rcx,%r14,8), %rax
movl 0xc(%rax), %r8d
movq (%rsi,%r15,8), %r9
movl 0x20(%r9), %edi
cmpl %edi, %r8d
jg 0x196ea
leaq (%rcx,%r14,8), %rcx
leaq (%rsi,%r15,8), %rsi
cmpl 0xc(%r9), %r8d
movq %rsi, %r8
cmovgq %rcx, %r8
movq (%r8), %r8
movl 0xc(%r8), %r12d
cmpl %edi, 0x20(%rax)
cmovlq %rcx, %rsi
movq (%rsi), %rax
movl 0x20(%rax), %ebp
movl %ebp, %ebx
subl %r12d, %ebx
jl 0x196c7
movq (%r10), %rax
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x19795
movq (%rax,%r15,8), %rdi
callq 0x172ac
movq %rax, %r13
movq %rax, 0x40(%rsp)
movl %r12d, 0xc(%rax)
movl %ebp, 0x20(%rax)
movl $0x18, %edi
callq 0x2180
movq (%rsp), %r10
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq (%r10), %rcx
movq 0x8(%r10), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x19795
movq (%rcx,%r15,8), %rcx
movq %rcx, (%rax)
movq 0x30(%rsp), %r11
movq (%r11), %rcx
movq 0x8(%r11), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x19789
movq (%rcx,%r14,8), %rcx
movq %rcx, 0x8(%rax)
incl %ebx
movl %ebx, 0x10(%rax)
movq %rax, 0x48(%r13)
movq 0x28(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x196ad
movq %r13, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x196c0
leaq 0x40(%rsp), %rdx
callq 0xb976
movq 0x30(%rsp), %r11
movq (%rsp), %r10
cmpb $0x0, 0x1c(%rsp)
je 0x196ea
incq %r14
movq (%r11), %rcx
movq 0x8(%r11), %rax
subq %rcx, %rax
movq %rax, %rdx
sarq $0x3, %rdx
shrq $0x3, %rax
cltq
cmpq %rax, %r14
jl 0x19594
incq %r15
movq (%r10), %rcx
movq 0x8(%r10), %rax
subq %rcx, %rax
movq %rax, %rdx
sarq $0x3, %rdx
shrq $0x3, %rax
cltq
cmpq %rax, %r15
jl 0x194ed
movq 0x20(%rsp), %rsi
incq %rsi
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rsi
jl 0x19475
movq 0x38(%rsp), %rdi
callq 0x1608f
movq 0x10(%rsp), %rdi
callq 0x16c1c
movq 0x8(%rsp), %rdi
callq 0x16c1c
movq 0x28(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1415e(%rip), %rdi     # 0x2d8d6
jmp 0x197ae
leaq 0x14155(%rip), %rdi     # 0x2d8d6
movq %r15, %rsi
movq %rcx, %rdx
jmp 0x197ae
leaq 0x14146(%rip), %rdi     # 0x2d8d6
movq %r14, %rsi
jmp 0x197ae
leaq 0x1413a(%rip), %rdi     # 0x2d8d6
movq %r15, %rsi
jmp 0x197ae
leaq 0x1412e(%rip), %rdi     # 0x2d8d6
movq %r15, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2270