// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movl %edx, 0x24(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
movl $0x18, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x38(%rsp)
movq $0x0, 0x10(%rax)
leaq -0x217e(%rip), %rsi     # 0x16f04
movq %rbx, %rdi
callq 0x15c38
movq %r14, %rdi
callq 0x163f1
movq %rax, %r14
movq %rbx, 0x40(%rsp)
movq %rbx, %rdi
callq 0x163f1
movq %rax, 0x10(%rsp)
movq %r14, 0x28(%rsp)
movq 0x18(%r14), %rcx
movq (%rcx), %rax
movq %rcx, 0x48(%rsp)
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x1935f
xorl %esi, %esi
cmpq %rsi, %rdx
jbe 0x1939d
movq 0x10(%rsp), %rcx
movq 0x18(%rcx), %r14
movq %rsi, 0x30(%rsp)
movq (%rax,%rsi,8), %rsi
movq %r14, %rdi
callq 0x26ed0
movq 0x8(%r14), %rcx
subq (%r14), %rcx
shrq $0x3, %rcx
cmpl %ecx, %eax
jge 0x19331
movq 0x28(%rsp), %rcx
movq (%rcx), %rcx
movq 0x30(%rsp), %rdx
movq (%rcx,%rdx,8), %r10
movq (%r10), %rcx
movq 0x8(%r10), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x19331
movq 0x10(%rsp), %rsi
movq (%rsi), %rsi
cltq
movq (%rsi,%rax,8), %rax
movq %rax, 0x18(%rsp)
xorl %r15d, %r15d
movq %r10, 0x8(%rsp)
cmpq %r15, %rdx
jbe 0x19391
movq (%rcx,%r15,8), %rax
movl 0xc(%rax), %edi
movq 0x18(%rsp), %rbx
movq 0x8(%rbx), %rcx
subq (%rbx), %rcx
shrq $0x3, %rcx
decl %ecx
movq %rbx, %rsi
xorl %edx, %edx
callq 0x167a8
movq (%rbx), %r8
movq 0x8(%rbx), %r9
movq %r9, %rdx
subq %r8, %rdx
sarq $0x3, %rdx
testl %eax, %eax
jle 0x191cf
movq 0x8(%rsp), %r10
movl %eax, %esi
cmpq %rsi, %rdx
jbe 0x1939d
movq (%r10), %rdi
movq 0x8(%r10), %rcx
subq %rdi, %rcx
sarq $0x3, %rcx
cmpq %r15, %rcx
jbe 0x193a6
movq (%r8,%rsi,8), %rcx
movq 0x38(%rcx), %rcx
movl 0x4(%rcx), %ecx
movq (%rdi,%r15,8), %rsi
cmpl 0xc(%rsi), %ecx
jle 0x191d4
leal -0x1(%rax), %ecx
cmpl $0x1, %eax
movl %ecx, %eax
jg 0x1918b
xorl %eax, %eax
jmp 0x191d4
movq 0x8(%rsp), %r10
cmpl %edx, %eax
jge 0x1930e
movslq %eax, %r14
cmpq %r14, %rdx
jbe 0x193b5
movq (%r10), %rcx
movq 0x8(%r10), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x19391
movq (%r8,%r14,8), %rax
movl 0xc(%rax), %edx
movq (%rcx,%r15,8), %rdi
movl 0x20(%rdi), %ecx
cmpl %ecx, %edx
jg 0x1930e
cmpl 0xc(%rdi), %edx
movq %rdi, %rdx
cmovgq %rax, %rdx
cmpl %ecx, 0x20(%rax)
cmovgeq %rdi, %rax
movl 0xc(%rdx), %r12d
movl 0x20(%rax), %ebx
movl %ebx, %ebp
subl %r12d, %ebp
jl 0x192ef
callq 0x16f1f
movq %rax, %r13
movq %rax, 0x50(%rsp)
movl %r12d, 0xc(%rax)
movl %ebx, 0x20(%rax)
movl $0x18, %edi
callq 0x2180
movq 0x8(%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 0x19391
movq (%rcx,%r15,8), %rcx
movq %rcx, (%rax)
movq 0x18(%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x193b5
movq (%rcx,%r14,8), %rcx
movq %rcx, 0x8(%rax)
incl %ebp
movl %ebp, 0x10(%rax)
movq %rax, 0x48(%r13)
movq 0x38(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x192cd
movq %r13, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x192dc
leaq 0x50(%rsp), %rdx
callq 0xb74c
movq 0x8(%rsp), %r10
cmpb $0x0, 0x24(%rsp)
je 0x1930e
movq 0x18(%rsp), %rax
movq (%rax), %r8
movq 0x8(%rax), %r9
incq %r14
movq %r9, %rax
subq %r8, %rax
movq %rax, %rdx
sarq $0x3, %rdx
shrq $0x3, %rax
cltq
cmpq %rax, %r14
jl 0x191df
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 0x19145
movq 0x30(%rsp), %rsi
incq %rsi
movq 0x48(%rsp), %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 0x190cd
movq 0x40(%rsp), %rdi
callq 0x15d42
movq 0x28(%rsp), %rdi
callq 0x16877
movq 0x10(%rsp), %rdi
callq 0x16877
movq 0x38(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x13538(%rip), %rdi     # 0x2c8d0
movq %r15, %rsi
jmp 0x193bf
leaq 0x1352c(%rip), %rdi     # 0x2c8d0
jmp 0x193bf
leaq 0x13523(%rip), %rdi     # 0x2c8d0
movq %r15, %rsi
movq %rcx, %rdx
jmp 0x193bf
leaq 0x13514(%rip), %rdi     # 0x2c8d0
movq %r14, %rsi
xorl %eax, %eax
callq 0x2270