// File: 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:
subq $0xc8, %rsp
movb %dl, %al
movq %rdi, 0xc0(%rsp)
movq %rsi, 0xb8(%rsp)
andb $0x1, %al
movb %al, 0xb7(%rsp)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x58(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x58(%rsp), %rdi
callq 0xc670
movq 0x58(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0xb8(%rsp), %rdi
leaq -0x2d36(%rip), %rsi     # 0x2a3a0
callq 0x28440
movq 0xc0(%rsp), %rdi
callq 0x29200
movq %rax, 0xa0(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x29200
movq %rax, 0x98(%rsp)
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0x54(%rsp)
movq 0xa0(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x54(%rsp), %eax
cmpl %ecx, %eax
jge 0x2d570
movq 0x98(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x48(%rsp)
movq 0xa0(%rsp), %rax
movq 0x18(%rax), %rdi
movslq 0x94(%rsp), %rsi
callq 0xc240
movq 0x48(%rsp), %rdi
movq (%rax), %rsi
callq 0x47cf0
movl %eax, 0x90(%rsp)
movl 0x90(%rsp), %eax
movl %eax, 0x50(%rsp)
movq 0x98(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x50(%rsp), %eax
cmpl %ecx, %eax
jge 0x2d556
movq 0xa0(%rsp), %rax
movq (%rax), %rax
movslq 0x94(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x88(%rsp)
movq 0x98(%rsp), %rax
movq (%rax), %rax
movslq 0x90(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x80(%rsp)
movl $0x0, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
movl %eax, 0x44(%rsp)
movq 0x88(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0x44(%rsp), %eax
cmpl %ecx, %eax
jge 0x2d554
movq 0x88(%rsp), %rdi
movslq 0x7c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x34(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x80(%rsp), %rdi
callq 0xc680
movl 0x34(%rsp), %edi
movq 0x38(%rsp), %rsi
movl %eax, %ecx
subl $0x1, %ecx
xorl %edx, %edx
callq 0x297a0
movl %eax, 0x78(%rsp)
xorl %eax, %eax
cmpl $0x0, 0x78(%rsp)
movb %al, 0x33(%rsp)
jle 0x2d2b0
movq 0x80(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movq 0x38(%rax), %rax
movl 0x4(%rax), %eax
movl %eax, 0x2c(%rsp)
movq 0x88(%rsp), %rdi
movslq 0x7c(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x2c(%rsp), %eax
movq (%rcx), %rcx
cmpl 0xc(%rcx), %eax
setg %al
movb %al, 0x33(%rsp)
movb 0x33(%rsp), %al
testb $0x1, %al
jne 0x2d2ba
jmp 0x2d2c7
movl 0x78(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x2d25d
movl $0x0, 0x74(%rsp)
movl $0x0, 0x70(%rsp)
movl 0x78(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x80(%rsp), %rdi
callq 0xc680
movl 0x24(%rsp), %ecx
movl %eax, %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, 0x2b(%rsp)
jge 0x2d33e
movq 0x80(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x20(%rsp)
movq 0x88(%rsp), %rdi
movslq 0x7c(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x20(%rsp), %eax
movq (%rcx), %rcx
cmpl 0x20(%rcx), %eax
setle %al
movb %al, 0x2b(%rsp)
movb 0x2b(%rsp), %al
testb $0x1, %al
jne 0x2d34b
jmp 0x2d542
movq 0x80(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x1c(%rsp)
movq 0x88(%rsp), %rdi
movslq 0x7c(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x1c(%rsp), %eax
movq (%rcx), %rcx
cmpl 0xc(%rcx), %eax
jle 0x2d3a6
movq 0x80(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x18(%rsp)
jmp 0x2d3c2
movq 0x88(%rsp), %rdi
movslq 0x7c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x18(%rsp)
movl 0x18(%rsp), %eax
movl %eax, 0x74(%rsp)
movq 0x80(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x14(%rsp)
movq 0x88(%rsp), %rdi
movslq 0x7c(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x14(%rsp), %eax
movq (%rcx), %rcx
cmpl 0x20(%rcx), %eax
jge 0x2d425
movq 0x80(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x10(%rsp)
jmp 0x2d441
movq 0x88(%rsp), %rdi
movslq 0x7c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x70(%rsp)
movl 0x70(%rsp), %eax
cmpl 0x74(%rsp), %eax
jl 0x2d532
movq 0x88(%rsp), %rdi
movslq 0x7c(%rsp), %rsi
callq 0xc800
movq (%rax), %rdi
callq 0x2a410
movq %rax, 0x68(%rsp)
movl 0x74(%rsp), %ecx
movq 0x68(%rsp), %rax
movl %ecx, 0xc(%rax)
movl 0x70(%rsp), %ecx
movq 0x68(%rsp), %rax
movl %ecx, 0x20(%rax)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x8(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x8(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x88(%rsp), %rdi
movslq 0x7c(%rsp), %rsi
callq 0xc800
movq (%rax), %rcx
movq 0x60(%rsp), %rax
movq %rcx, (%rax)
movq 0x80(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq (%rax), %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x8(%rax)
movl 0x70(%rsp), %ecx
subl 0x74(%rsp), %ecx
addl $0x1, %ecx
movq 0x60(%rsp), %rax
movl %ecx, 0x10(%rax)
movq 0x60(%rsp), %rcx
movq 0x68(%rsp), %rax
movq %rcx, 0x48(%rax)
movq 0xa8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0xc8c0
testb $0x1, 0xb7(%rsp)
jne 0x2d530
jmp 0x2d542
jmp 0x2d532
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x2d2d7
jmp 0x2d544
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x2d1ea
jmp 0x2d558
jmp 0x2d558
jmp 0x2d55a
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x2d110
movq 0xb8(%rsp), %rdi
callq 0x28620
movq 0xa0(%rsp), %rdi
callq 0x29990
movq 0x98(%rsp), %rdi
callq 0x29990
movq 0xa8(%rsp), %rax
addq $0xc8, %rsp
retq
nopw (%rax,%rax)