// File: gersteinlab[P]MUSIC/src/ms_annot_region_tools.cpp
// Function: exclude_annot_regions(std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, std::vector<t_annot_region*, std::allocator<t_annot_region*>>*)
vector<t_annot_region*>* exclude_annot_regions(vector<t_annot_region*>* annot_regions1,
													vector<t_annot_region*>* annot_regions2)
{
	vector<t_annot_region*>* excluded_regions = new vector<t_annot_region*>();

	vector<t_annot_region*>* merged_regs2 = merge_annot_regions(annot_regions2, 0, false);

	// 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(merged_regs2);

	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));

		vector<t_annot_region*>* cur_chr_regs1 = restructured_regs1->regions_per_chrom[i_chr1];
		if(i_chr2 < (int)restructured_regs2->chr_ids->size())
		{			
			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);

				while(i_leftmost_reg > 0 && 
					cur_chr_regs2->at(i_leftmost_reg)->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;
				while(!region_overlaps && 
					i_leftmost_reg < (int)cur_chr_regs2->size() &&
					cur_chr_regs2->at(i_leftmost_reg)->start < cur_chr_regs1->at(i_reg1)->end)
				{
					int ol_start = MAX(cur_chr_regs2->at(i_leftmost_reg)->start, cur_chr_regs1->at(i_reg1)->start);
					int 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.
						region_overlaps = true;
					}
					i_leftmost_reg++;
				}

				if(!region_overlaps)
				{
					t_annot_region* new_excluded_region = duplicate_region(cur_chr_regs1->at(i_reg1));
					new_excluded_region->data = cur_chr_regs1->at(i_reg1);

					excluded_regions->push_back(new_excluded_region);
				}
			} // i_reg1 loop.
		} // i_chr2 check.
		else
		{
			// chr2 does not exist in region2 list, all the regions in this chromosome for region1 list are excluded from regions2.
			for(int i_reg1 = 0; i_reg1 < (int)cur_chr_regs1->size(); i_reg1++)
			{
				t_annot_region* new_excluded_region = duplicate_region(cur_chr_regs1->at(i_reg1));
				new_excluded_region->data = cur_chr_regs1->at(i_reg1);

				excluded_regions->push_back(new_excluded_region);
			} // i_reg1 loop.
		}
	} // i_chr1 loop.

	// Free memory for merged regions.
	delete_annot_regions(merged_regs2);

	delete_restructured_annot_regions(restructured_regs1);
	delete_restructured_annot_regions(restructured_regs2);

	return(excluded_regions);
}

// Assembly:
subq $0xd8, %rsp
movq %rdi, 0xd0(%rsp)
movq %rsi, 0xc8(%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, 0xc0(%rsp)
movq 0xc8(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x2f380
movq %rax, 0xb8(%rsp)
movq 0xd0(%rsp), %rdi
callq 0x29200
movq %rax, 0xb0(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x29200
movq %rax, 0xa8(%rsp)
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x54(%rsp)
movq 0xb0(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x54(%rsp), %eax
cmpl %ecx, %eax
jge 0x2f33b
movq 0xa8(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x48(%rsp)
movq 0xb0(%rsp), %rax
movq 0x18(%rax), %rdi
movslq 0xa4(%rsp), %rsi
callq 0xc240
movq 0x48(%rsp), %rdi
movq (%rax), %rsi
callq 0x47cf0
movl %eax, 0xa0(%rsp)
movq 0xb0(%rsp), %rax
movq (%rax), %rax
movslq 0xa4(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x98(%rsp)
movl 0xa0(%rsp), %eax
movl %eax, 0x50(%rsp)
movq 0xa8(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x50(%rsp), %eax
cmpl %ecx, %eax
jge 0x2f29d
movq 0xa8(%rsp), %rax
movq (%rax), %rax
movslq 0xa0(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x90(%rsp)
movl $0x0, 0x8c(%rsp)
movl 0x8c(%rsp), %eax
movl %eax, 0x44(%rsp)
movq 0x98(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0x44(%rsp), %eax
cmpl %ecx, %eax
jge 0x2f298
movq 0x98(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x34(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x90(%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, 0x88(%rsp)
xorl %eax, %eax
cmpl $0x0, 0x88(%rsp)
movb %al, 0x33(%rsp)
jle 0x2f02b
movq 0x90(%rsp), %rdi
movslq 0x88(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x2c(%rsp)
movq 0x98(%rsp), %rdi
movslq 0x8c(%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 0x2f035
jmp 0x2f048
movl 0x88(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x88(%rsp)
jmp 0x2efd3
movb $0x0, 0x87(%rsp)
xorl %eax, %eax
testb $0x1, 0x87(%rsp)
movb %al, 0x2b(%rsp)
jne 0x2f0d0
movl 0x88(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x90(%rsp), %rdi
callq 0xc680
movl 0x24(%rsp), %ecx
movl %eax, %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, 0x2b(%rsp)
jge 0x2f0d0
movq 0x90(%rsp), %rdi
movslq 0x88(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x20(%rsp)
movq 0x98(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x20(%rsp), %eax
movq (%rcx), %rcx
cmpl 0x20(%rcx), %eax
setl %al
movb %al, 0x2b(%rsp)
movb 0x2b(%rsp), %al
testb $0x1, %al
jne 0x2f0dd
jmp 0x2f221
movq 0x90(%rsp), %rdi
movslq 0x88(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x1c(%rsp)
movq 0x98(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x1c(%rsp), %eax
movq (%rcx), %rcx
cmpl 0xc(%rcx), %eax
jle 0x2f141
movq 0x90(%rsp), %rdi
movslq 0x88(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x18(%rsp)
jmp 0x2f160
movq 0x98(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x18(%rsp)
movl 0x18(%rsp), %eax
movl %eax, 0x80(%rsp)
movq 0x90(%rsp), %rdi
movslq 0x88(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x14(%rsp)
movq 0x98(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x14(%rsp), %eax
movq (%rcx), %rcx
cmpl 0x20(%rcx), %eax
jge 0x2f1cf
movq 0x90(%rsp), %rdi
movslq 0x88(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x10(%rsp)
jmp 0x2f1ee
movq 0x98(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
cmpl 0x80(%rsp), %eax
jl 0x2f20b
movb $0x1, 0x87(%rsp)
movl 0x88(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x88(%rsp)
jmp 0x2f050
testb $0x1, 0x87(%rsp)
jne 0x2f280
movq 0x98(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0xc800
movq (%rax), %rdi
callq 0x2a410
movq %rax, 0x70(%rsp)
movq 0x98(%rsp), %rdi
movslq 0x8c(%rsp), %rsi
callq 0xc800
movq (%rax), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x48(%rax)
movq 0xc0(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0xc8c0
jmp 0x2f282
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x2ef57
jmp 0x2f323
movl $0x0, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x98(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x2f321
movq 0x98(%rsp), %rdi
movslq 0x6c(%rsp), %rsi
callq 0xc800
movq (%rax), %rdi
callq 0x2a410
movq %rax, 0x60(%rsp)
movq 0x98(%rsp), %rdi
movslq 0x6c(%rsp), %rsi
callq 0xc800
movq (%rax), %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x48(%rax)
movq 0xc0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xc8c0
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x2f2a5
jmp 0x2f323
jmp 0x2f325
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x2ee7a
movq 0xb8(%rsp), %rdi
callq 0x2f560
movq 0xb0(%rsp), %rdi
callq 0x29990
movq 0xa8(%rsp), %rdi
callq 0x29990
movq 0xc0(%rsp), %rax
addq $0xd8, %rsp
retq
nopw %cs:(%rax,%rax)