// File: /workspace/github_cpp_makefile_star2_O2/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, 0x1c(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
pushq $0x18
popq %rdi
callq 0x2170
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x30(%rsp)
andq $0x0, 0x10(%rax)
leaq -0x1c73(%rip), %rsi     # 0x13497
movq %r14, %rdi
callq 0x123af
movq %rbx, %rdi
callq 0x12b99
movq %rax, %rbx
movq %r14, 0x40(%rsp)
movq %r14, %rdi
callq 0x12b99
movq %rax, 0x10(%rsp)
movq %rbx, 0x20(%rsp)
movq 0x18(%rbx), %rdi
xorl %esi, %esi
movq %rdi, 0x48(%rsp)
movq 0x8(%rdi), %rax
subq (%rdi), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rsi
jge 0x153bc
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rbx
movq %rsi, 0x28(%rsp)
callq 0x7342
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x21152
movq 0x8(%rbx), %rcx
subq (%rbx), %rcx
shrq $0x3, %rcx
cmpl %ecx, %eax
jge 0x153aa
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
movq 0x28(%rsp), %rdx
movq (%rcx,%rdx,8), %rdi
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
cltq
movq (%rcx,%rax,8), %r12
xorl %r15d, %r15d
movq %rdi, 0x8(%rsp)
movq %r12, 0x50(%rsp)
movq 0x8(%rdi), %rax
subq (%rdi), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r15
jge 0x153aa
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %edi
movq 0x8(%r12), %rcx
subq (%r12), %rcx
shrq $0x3, %rcx
decl %ecx
movq %r12, %rsi
xorl %edx, %edx
callq 0x12dee
movl %eax, %r13d
movl %eax, %ebx
movl %eax, %ebp
sarl $0x1f, %ebp
andl %eax, %ebp
incl %r13d
testl %ebx, %ebx
jle 0x15236
movq %r12, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
movq 0x38(%rax), %rax
movl 0x4(%rax), %r14d
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
decq %rbx
decl %r13d
cmpl 0xc(%rax), %r14d
jg 0x151fe
jmp 0x15239
movl %ebp, %r13d
movslq %r13d, %r13
movq 0x8(%r12), %rax
subq (%r12), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r13
jge 0x1539d
movq %r12, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %ebx
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
cmpl 0x20(%rax), %ebx
jg 0x1539d
movq %r12, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %ebx
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
cmpl 0xc(%rax), %ebx
jle 0x152af
movq %r12, %rdi
movq %r13, %rsi
jmp 0x152b5
movq %r14, %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %ebx
movq %r12, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %ebp
movq %r14, %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
cmpl 0x20(%rax), %ebp
jge 0x152ec
movq %r12, %rdi
movq %r13, %rsi
jmp 0x152f2
movq %r14, %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %r14d
movl %r14d, %ebp
subl %ebx, %ebp
jl 0x15395
movq %r15, %r12
movq 0x8(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rdi
callq 0x134b2
movq %rax, 0x38(%rsp)
movl %ebx, 0xc(%rax)
movl %r14d, 0x20(%rax)
pushq $0x18
popq %rdi
callq 0x2170
movq %rax, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %r15, %rdi
movq %r12, %r15
movq 0x50(%rsp), %r12
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
movq %rax, (%rbx)
movq %r12, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movq %rax, 0x8(%rbx)
incl %ebp
movl %ebp, 0x10(%rbx)
movq 0x38(%rsp), %rax
movq %rbx, 0x48(%rax)
movq 0x30(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x7448
cmpb $0x0, 0x1c(%rsp)
je 0x1539d
incq %r13
jmp 0x1523c
incq %r15
movq 0x8(%rsp), %rdi
jmp 0x151b2
movq 0x28(%rsp), %rsi
incq %rsi
movq 0x48(%rsp), %rdi
jmp 0x1513f
movq 0x40(%rsp), %rdi
callq 0x124ba
movq 0x20(%rsp), %rdi
callq 0x12f32
movq 0x10(%rsp), %rdi
callq 0x12f32
movq 0x30(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq