// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movq %rsi, %r14
movq %rdi, %r15
movl $0x18, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x8(%rsp)
movq $0x0, 0x10(%rax)
movq %r14, %rdi
xorl %esi, %esi
callq 0x1b5ff
movq %rax, %rbx
movq %r15, %rdi
callq 0x163f1
movq %rax, %r14
movq %rbx, 0x20(%rsp)
movq %rbx, %rdi
callq 0x163f1
movq %rax, (%rsp)
movq %r14, 0x18(%rsp)
movq 0x18(%r14), %rcx
movq (%rcx), %rax
movq %rcx, 0x28(%rsp)
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x1a642
xorl %ebx, %ebx
cmpq %rbx, %rdx
jbe 0x1a6a3
movq (%rsp), %rcx
movq 0x18(%rcx), %r14
movq (%rax,%rbx,8), %rsi
movq %r14, %rdi
callq 0x26ed0
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movq %rbx, 0x30(%rsp)
movq (%rcx,%rbx,8), %r12
movq 0x8(%r14), %rcx
subq (%r14), %rcx
shrq $0x3, %rcx
cmpl %ecx, %eax
jge 0x1a576
movq (%r12), %r8
movq 0x8(%r12), %rcx
subq %r8, %rcx
sarq $0x3, %rcx
testl %ecx, %ecx
jle 0x1a614
movq (%rsp), %rdx
movq (%rdx), %rdx
cltq
movq (%rdx,%rax,8), %r15
xorl %r14d, %r14d
cmpq %r14, %rcx
jbe 0x1a673
movq (%r8,%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 0x167a8
movq (%r15), %rdi
movq 0x8(%r15), %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
testl %eax, %eax
jle 0x1a47a
movl %eax, %esi
cmpq %rsi, %rdx
jbe 0x1a689
movq (%r12), %r8
movq 0x8(%r12), %rcx
subq %r8, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
jbe 0x1a673
movq (%rdi,%rsi,8), %rcx
movl 0x20(%rcx), %ecx
movq (%r8,%r14,8), %rsi
cmpl 0xc(%rsi), %ecx
jle 0x1a47a
leal -0x1(%rax), %ecx
cmpl $0x1, %eax
movl %ecx, %eax
jg 0x1a43a
xorl %eax, %eax
movslq %eax, %rsi
cmpl %edx, %esi
movl %edx, %eax
cmovgl %esi, %eax
cltq
movq %rsi, %r9
cmpq %r9, %rax
je 0x1a4e7
cmpq %r9, %rdx
jbe 0x1a682
movq (%r12), %r8
movq 0x8(%r12), %rcx
subq %r8, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
jbe 0x1a673
movq (%rdi,%r9,8), %r10
movl 0xc(%r10), %ebx
movq (%r8,%r14,8), %r11
movl 0x20(%r11), %ebp
cmpl %ebp, %ebx
jge 0x1a4f7
movq %r10, %r13
cmpl 0xc(%r11), %ebx
jg 0x1a4d0
movq %r11, %r13
cmpl %ebp, 0x20(%r10)
cmovlq %r10, %r11
movl 0x20(%r11), %r10d
incq %r9
cmpl 0xc(%r13), %r10d
jl 0x1a489
jmp 0x1a562
movq (%r12), %r8
movq 0x8(%r12), %rcx
subq %r8, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
jbe 0x1a673
movq (%r8,%r14,8), %rdi
callq 0x16f1f
movq %rax, 0x10(%rsp)
movq (%r12), %rcx
movq 0x8(%r12), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x1a697
movq (%rcx,%r14,8), %rcx
movq %rcx, 0x48(%rax)
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x1a548
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x1a552
leaq 0x10(%rsp), %rdx
callq 0xb74c
movq (%r12), %r8
movq 0x8(%r12), %rcx
subq %r8, %rcx
sarq $0x3, %rcx
incq %r14
movslq %ecx, %rax
cmpq %rax, %r14
jl 0x1a401
jmp 0x1a614
movq (%r12), %rax
movq 0x8(%r12), %rdx
movq %rdx, %rcx
subq %rax, %rcx
shrq $0x3, %rcx
testl %ecx, %ecx
jle 0x1a614
xorl %r14d, %r14d
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x1a697
movq (%rax,%r14,8), %rdi
callq 0x16f1f
movq %rax, 0x10(%rsp)
movq (%r12), %rcx
movq 0x8(%r12), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x1a697
movq (%rcx,%r14,8), %rcx
movq %rcx, 0x48(%rax)
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x1a5ec
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x1a5f6
leaq 0x10(%rsp), %rdx
callq 0xb74c
incq %r14
movq (%r12), %rax
movq 0x8(%r12), %rdx
movq %rdx, %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r14
jl 0x1a594
movq 0x30(%rsp), %rbx
incq %rbx
movq 0x28(%rsp), %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, %rbx
jl 0x1a398
movq 0x20(%rsp), %rdi
callq 0x1a88a
movq 0x18(%rsp), %rdi
callq 0x16877
movq (%rsp), %rdi
callq 0x16877
movq 0x8(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x12256(%rip), %rdi     # 0x2c8d0
movq %r14, %rsi
movq %rcx, %rdx
jmp 0x1a690
cmpq %rsi, %rdx
cmovaq %rdx, %rsi
leaq 0x12240(%rip), %rdi     # 0x2c8d0
xorl %eax, %eax
callq 0x2270
leaq 0x12232(%rip), %rdi     # 0x2c8d0
movq %r14, %rsi
jmp 0x1a690
leaq 0x12226(%rip), %rdi     # 0x2c8d0
movq %rbx, %rsi
jmp 0x1a690