// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r14
movq %rdi, %r15
pushq $0x18
popq %rdi
callq 0x2170
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x10(%rsp)
andq $0x0, 0x10(%rax)
xorl %ebx, %ebx
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x16489
movq %rax, %r14
movq %r15, %rdi
callq 0x12b99
movq %rax, %r15
movq %r14, 0x28(%rsp)
movq %r14, %rdi
callq 0x12b99
movq %rax, %r13
movq %r15, 0x20(%rsp)
movq 0x18(%r15), %rdi
movq %rax, 0x38(%rsp)
movq %rdi, 0x30(%rsp)
movq 0x8(%rdi), %rax
subq (%rdi), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbx
jge 0x16459
movq 0x18(%r13), %r14
movq %rbx, %rsi
callq 0x7342
movq (%rax), %rsi
movq %r14, %rdi
callq 0x21152
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
movq %rbx, 0x40(%rsp)
movq (%rcx,%rbx,8), %r12
movq 0x8(%r14), %rcx
subq (%r14), %rcx
shrq $0x3, %rcx
cmpl %ecx, %eax
jge 0x163e8
movq (%r13), %rcx
cltq
movq (%rcx,%rax,8), %r13
xorl %ebp, %ebp
movq 0x8(%r12), %rax
subq (%r12), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbp
jge 0x16442
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %edi
movq 0x8(%r13), %rcx
subq (%r13), %rcx
shrq $0x3, %rcx
decl %ecx
movq %r13, %rsi
xorl %edx, %edx
callq 0x12dee
movl %eax, %r14d
movl %eax, %r15d
sarl $0x1f, %eax
andl %r14d, %eax
movl %eax, 0x1c(%rsp)
incl %r14d
testl %r15d, %r15d
jle 0x162ca
movq %r13, %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %ebx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
decq %r15
decl %r14d
cmpl 0xc(%rax), %ebx
jg 0x16299
jmp 0x162cf
movl 0x1c(%rsp), %r14d
movslq %r14d, %r14
xorl %eax, %eax
testb $0x1, %al
jne 0x163e0
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r14
jge 0x163a2
movq %r13, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %ebx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
cmpl 0x20(%rax), %ebx
jge 0x163a2
movq %r13, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %ebx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
cmpl 0xc(%rax), %ebx
jle 0x16347
movq %r13, %rdi
movq %r14, %rsi
jmp 0x1634d
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %ebx
movq %r13, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %r15d
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
cmpl 0x20(%rax), %r15d
jge 0x16386
movq %r13, %rdi
movq %r14, %rsi
jmp 0x1638c
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
cmpl %ebx, 0x20(%rax)
setge %al
incq %r14
jmp 0x162d4
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rdi
callq 0x134b2
movq %rax, 0x8(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movq 0x8(%rsp), %rcx
movq %rax, 0x48(%rcx)
movq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x7448
incq %rbp
jmp 0x16245
xorl %r14d, %r14d
movq 0x8(%r12), %rax
subq (%r12), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r14
jge 0x16442
movq %r12, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rdi
callq 0x134b2
movq %rax, 0x8(%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movq 0x8(%rsp), %rcx
movq %rax, 0x48(%rcx)
movq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x7448
incq %r14
jmp 0x163eb
movq 0x40(%rsp), %rbx
incq %rbx
movq 0x38(%rsp), %r13
movq 0x30(%rsp), %rdi
jmp 0x161e8
movq 0x28(%rsp), %rdi
callq 0x165a8
movq 0x20(%rsp), %rdi
callq 0x12f32
movq %r13, %rdi
callq 0x12f32
movq 0x10(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq