// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
movq %rsi, %r14
movq %rdi, %r15
movl $0x18, %edi
callq 0x2180
movq %rax, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %r14, %rdi
xorl %esi, %esi
callq 0x1ba25
movq %rax, %r14
movq %r15, %rdi
callq 0x16742
movq %rax, %r15
movq %r14, 0x10(%rsp)
movq %r14, %rdi
callq 0x16742
movq %rax, (%rsp)
movq 0x18(%r15), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x1aaa4
xorl %r12d, %r12d
movq %r15, 0x18(%rsp)
cmpq %r12, %rdx
jbe 0x1ab10
movq (%rsp), %r14
movq 0x18(%r14), %rdi
movq (%rax,%r12,8), %rsi
callq 0x274fc
movq (%r15), %rcx
movq %r12, 0x20(%rsp)
movq (%rcx,%r12,8), %r12
movq 0x18(%r14), %rcx
movq 0x8(%rcx), %rdx
subq (%rcx), %rdx
shrq $0x3, %rdx
cmpl %edx, %eax
jge 0x1a9d9
movq (%r12), %rcx
movq 0x8(%r12), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x1aa72
movq (%rsp), %rsi
movq (%rsi), %rsi
cltq
movq (%rsi,%rax,8), %r15
xorl %r14d, %r14d
cmpq %r14, %rdx
jbe 0x1aaff
movq (%rcx,%r14,8), %rax
movl 0xc(%rax), %edi
movq 0x8(%r15), %rcx
subq (%r15), %rcx
shrq $0x3, %rcx
decl %ecx
movq %r15, %rsi
xorl %edx, %edx
callq 0x16b4d
testl %eax, %eax
jle 0x1a8a5
movq (%r15), %rdi
movq 0x8(%r15), %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
movl %eax, %esi
cmpq %rsi, %rdx
jbe 0x1aad8
movq (%r12), %r8
movq 0x8(%r12), %rcx
subq %r8, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
jbe 0x1aaf0
movq (%rdi,%rsi,8), %rcx
movl 0x20(%rcx), %ecx
movq (%r8,%r14,8), %rsi
cmpl 0xc(%rsi), %ecx
jle 0x1a8a5
leal -0x1(%rax), %ecx
cmpl $0x1, %eax
movl %ecx, %eax
jg 0x1a865
xorl %eax, %eax
movq (%r15), %rdi
movq 0x8(%r15), %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
movslq %eax, %rsi
cmpl %edx, %esi
movl %edx, %eax
cmovgl %esi, %eax
movslq %eax, %rcx
leaq (%rdi,%rsi,8), %rdi
xorl %r8d, %r8d
movq %rsi, %r9
cmpq %r9, %rcx
je 0x1a946
cmpq %r9, %rdx
jbe 0x1aad1
movq (%r12), %r10
movq 0x8(%r12), %rax
subq %r10, %rax
sarq $0x3, %rax
cmpq %r14, %rax
jbe 0x1aae1
movq (%rdi), %rax
movl 0xc(%rax), %ebp
movq (%r10,%r14,8), %r13
movl 0x20(%r13), %r11d
cmpl %r11d, %ebp
jge 0x1a946
leaq (%r10,%r14,8), %r10
cmpl 0xc(%r13), %ebp
movq %r10, %r13
cmovgq %rdi, %r13
movq (%r13), %r13
cmpl %r11d, 0x20(%rax)
cmovlq %rdi, %r10
movq (%r10), %rax
movl 0x20(%rax), %eax
cmpl 0xc(%r13), %eax
jl 0x1a933
movl $0x1, %r8d
jmp 0x1a937
movzbl %r8b, %r8d
incq %r9
addq $0x8, %rdi
testb $0x1, %r8b
je 0x1a8ca
jmp 0x1a9af
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x1aaff
movq (%rax,%r14,8), %rdi
callq 0x172ac
movq %rax, 0x8(%rsp)
movq (%r12), %rcx
movq 0x8(%r12), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x1aaff
movq (%rcx,%r14,8), %rcx
movq %rcx, 0x48(%rax)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x1a9a2
movq %rax, (%rsi)
addq $0x8, 0x8(%rbx)
jmp 0x1a9af
movq %rbx, %rdi
leaq 0x8(%rsp), %rdx
callq 0xb976
incq %r14
movq (%r12), %rcx
movq 0x8(%r12), %rax
subq %rcx, %rax
movq %rax, %rdx
sarq $0x3, %rdx
shrq $0x3, %rax
cltq
cmpq %rax, %r14
jl 0x1a82c
jmp 0x1aa72
movq 0x8(%r12), %rdx
movq %rdx, %rax
subq (%r12), %rax
shrq $0x3, %rax
testl %eax, %eax
jle 0x1aa72
xorl %r14d, %r14d
movq (%r12), %rax
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x1aaff
movq (%rax,%r14,8), %rdi
callq 0x172ac
movq %rax, 0x8(%rsp)
movq (%r12), %rcx
movq 0x8(%r12), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x1aaff
movq (%rcx,%r14,8), %rcx
movq %rcx, 0x48(%rax)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x1aa4b
movq %rax, (%rsi)
addq $0x8, 0x8(%rbx)
jmp 0x1aa58
movq %rbx, %rdi
leaq 0x8(%rsp), %rdx
callq 0xb976
movq 0x8(%r12), %rdx
movq %rdx, %rax
subq (%r12), %rax
incq %r14
shrq $0x3, %rax
cltq
cmpq %rax, %r14
jl 0x1a9f4
movq 0x20(%rsp), %r12
incq %r12
movq 0x18(%rsp), %r15
movq 0x18(%r15), %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, %r12
jl 0x1a7c7
movq 0x10(%rsp), %rdi
callq 0x1acd9
movq %r15, %rdi
callq 0x16c1c
movq (%rsp), %rdi
callq 0x16c1c
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq %rsi, %rdx
cmovaq %rdx, %rsi
leaq 0x12df7(%rip), %rdi     # 0x2d8d6
jmp 0x1ab09
leaq 0x12dee(%rip), %rdi     # 0x2d8d6
movq %r14, %rsi
movq %rax, %rdx
jmp 0x1ab09
leaq 0x12ddf(%rip), %rdi     # 0x2d8d6
movq %r14, %rsi
movq %rcx, %rdx
jmp 0x1ab09
leaq 0x12dd0(%rip), %rdi     # 0x2d8d6
movq %r14, %rsi
xorl %eax, %eax
callq 0x2270
leaq 0x12dbf(%rip), %rdi     # 0x2d8d6
movq %r12, %rsi
jmp 0x1ab09