// File: /workspace/github_cpp_makefile_star2_O1/gersteinlab[P]MUSIC/src/ms_annot_region_tools.cpp
// Function: subtract_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*>* subtract_annot_regions(vector<t_annot_region*>* regions1, vector<t_annot_region*>* regions2)
{
	if(regions1 == NULL)
	{
		return(NULL);
	}
	else if(regions2 == NULL)
	{
		vector<t_annot_region*>* subtracted_regions1 = new vector<t_annot_region*>();
		for(int i_reg = 0; i_reg < (int)regions1->size(); i_reg++)
		{
			subtracted_regions1->push_back(duplicate_region(regions1->at(i_reg)));
		} // i_reg loop.

		return(subtracted_regions1);
	}

	vector<t_annot_region*>* subtracted_regions1 = new vector<t_annot_region*>();

	vector<t_annot_region*>* merged_regions2 = merge_annot_regions(regions2, 0);

	// Restructure the lists.
	t_sorted_annot_region_lists* restructured_regions1 = restructure_annot_regions(regions1);
	t_sorted_annot_region_lists* restructured_regions2 = restructure_annot_regions(merged_regions2);

	for(int i_chr1 = 0; i_chr1 < (int)restructured_regions1->chr_ids->size(); i_chr1++)
	{
		vector<t_annot_region*>* cur_chr_regs1 = restructured_regions1->regions_per_chrom[i_chr1];

		int i_chr2 = t_string::get_i_str(restructured_regions2->chr_ids, restructured_regions1->chr_ids->at(i_chr1));
		
		// Check if this chromosome exists in the second list
		if(i_chr2 < (int)restructured_regions2->chr_ids->size())
		{
			vector<t_annot_region*>* cur_chr_regs2 = restructured_regions2->regions_per_chrom[i_chr2];

			for(int i_reg1 = 0; i_reg1 < (int)cur_chr_regs1->size(); i_reg1++)
			{
				t_annot_region* cur_rightmost_remaining_region = duplicate_region(cur_chr_regs1->at(i_reg1));

				int cur_left_i = locate_posn_region_per_region_starts(cur_rightmost_remaining_region->start, cur_chr_regs2, 0, (int)cur_chr_regs2->size() - 1);

				while(cur_left_i > 0 &&
					cur_chr_regs2->at(cur_left_i)->end > cur_chr_regs1->at(i_reg1)->start)
				{
					cur_left_i--;
				} // cur_left_i loop.

				//bool found_overlap = false;

				// Move over the region 1 and get rid of pieces.
				while(cur_left_i < (int)cur_chr_regs2->size() && 
					cur_chr_regs2->at(cur_left_i)->start <= cur_chr_regs1->at(i_reg1)->end &&
					cur_rightmost_remaining_region != NULL)
				{
					int ol_start = MAX(cur_rightmost_remaining_region->start, cur_chr_regs2->at(cur_left_i)->start);
					int ol_end = MIN(cur_rightmost_remaining_region->end, cur_chr_regs2->at(cur_left_i)->end);

					if(ol_start <= ol_end)
					{
						//found_overlap = true;
						// Region2 overlaps with region1. Find the remaining regions based on the overlap.

						// Add the remaining regions: These are the parts that are outside the 
						if(cur_rightmost_remaining_region->start < ol_start)
						{
							t_annot_region* new_remaining_region = get_empty_region();
							new_remaining_region->chrom = t_string::copy_me_str(cur_rightmost_remaining_region->chrom);
							new_remaining_region->start = cur_rightmost_remaining_region->start;
							new_remaining_region->end = ol_start-1;
							new_remaining_region->strand = cur_rightmost_remaining_region->strand;
							new_remaining_region->data = cur_chr_regs1->at(i_reg1);

							// Add the left side remaining part to the list.
							subtracted_regions1->push_back(new_remaining_region);
						}

						// Check the right side remaining region.
						if(cur_rightmost_remaining_region->end > ol_end)
						{
							t_annot_region* new_remaining_region = get_empty_region();
							new_remaining_region->chrom = t_string::copy_me_str(cur_rightmost_remaining_region->chrom);
							new_remaining_region->start = ol_end+1;
							new_remaining_region->end = cur_rightmost_remaining_region->end;
							new_remaining_region->strand = cur_rightmost_remaining_region->strand;
							new_remaining_region->data = cur_chr_regs1->at(i_reg1);

							// Free the memory for rightmost remaining region, it is going to be updated.
							delete_annot_regions(cur_rightmost_remaining_region);

							// Set the remaining region to the current remaining region.
							cur_rightmost_remaining_region = new_remaining_region;
						}
						else
						{
							// Nothing is left on the right side: There is overlap and the overlap's end extends beyond end of the remaining region.
							delete_annot_regions(cur_rightmost_remaining_region);
							cur_rightmost_remaining_region = NULL;
						}
					} // overlap check.

					cur_left_i++;
				} // cur_left_i loop.

				// Add the final remaining region.
				if(cur_rightmost_remaining_region != NULL)
				{
					t_annot_region* final_remaining_region = cur_rightmost_remaining_region;

					// Sets the data to point to the original regions where the remnant is coming from.
					final_remaining_region->data = cur_chr_regs1->at(i_reg1);
					subtracted_regions1->push_back(final_remaining_region);
				}
			} // i_reg1 loop.
		}
		else
		{
			for(int i_reg1 = 0; i_reg1 < (int)cur_chr_regs1->size(); i_reg1++)
			{
				t_annot_region* new_remaining_region = duplicate_region(cur_chr_regs1->at(i_reg1));
				subtracted_regions1->push_back(new_remaining_region);
			} // i_reg1 loop.
		} // i_chr2 check.
	} // i_chr1 loop.

	delete_restructured_annot_regions(restructured_regions1);
	delete_restructured_annot_regions(restructured_regions2);
	delete_annot_regions(merged_regions2);
	return(subtracted_regions1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
testq %rdi, %rdi
je 0x1b966
movq %rsi, %r15
movq %rdi, %r14
movl $0x18, %edi
callq 0x2180
testq %r15, %r15
movq %rax, 0x8(%rsp)
xorps %xmm0, %xmm0
je 0x1b96a
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %r15, %rdi
xorl %esi, %esi
callq 0x1ba25
movq %rax, %rbx
movq %r14, %rdi
callq 0x16742
movq %rax, %r14
movq %rbx, 0x20(%rsp)
movq %rbx, %rdi
callq 0x16742
movq %rax, 0x10(%rsp)
movq 0x18(%r14), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x1b948
xorl %esi, %esi
movq %r14, 0x28(%rsp)
cmpq %rsi, %rdx
jbe 0x1b9f0
movq (%r14), %rcx
movq (%rcx,%rsi,8), %rbx
movq 0x10(%rsp), %r14
movq 0x18(%r14), %rdi
movq %rsi, 0x30(%rsp)
movq (%rax,%rsi,8), %rsi
callq 0x274fc
movq 0x18(%r14), %rcx
movq 0x8(%rcx), %rdx
subq (%rcx), %rdx
shrq $0x3, %rdx
cmpl %edx, %eax
jge 0x1b8a8
movq (%rbx), %rcx
movq 0x8(%rbx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x1b916
movq 0x10(%rsp), %rsi
movq (%rsi), %rsi
cltq
movq (%rsi,%rax,8), %r15
xorl %r14d, %r14d
movq %r15, 0x18(%rsp)
cmpq %r14, %rdx
jbe 0x1b9e4
movq (%rcx,%r14,8), %rdi
callq 0x172ac
movq %rax, %r13
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 0x1b67d
movq (%r15), %rdi
movq 0x8(%r15), %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
movl %eax, %esi
cmpq %rsi, %rdx
jbe 0x1b9f0
movq (%rbx), %r8
movq 0x8(%rbx), %rcx
subq %r8, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
jbe 0x1b9f9
movq (%rdi,%rsi,8), %rcx
movl 0x20(%rcx), %ecx
movq (%r8,%r14,8), %rsi
cmpl 0xc(%rsi), %ecx
jle 0x1b67d
leal -0x1(%rax), %ecx
cmpl $0x1, %eax
movl %ecx, %eax
jg 0x1b63f
xorl %eax, %eax
movq (%r15), %rcx
movq 0x8(%r15), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpl %edx, %eax
jge 0x1b83a
movslq %eax, %rbp
cmpq %rbp, %rdx
jbe 0x1ba08
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
subq %rsi, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x1b9e4
movq (%rcx,%rbp,8), %rax
movl 0xc(%rax), %edx
movq (%rsi,%r14,8), %rcx
cmpl 0x20(%rcx), %edx
jg 0x1b83a
testq %r13, %r13
je 0x1b83a
movl 0xc(%r13), %ecx
movl 0x20(%r13), %esi
cmpl %edx, %ecx
movq %rax, %rdx
cmovgq %r13, %rdx
movl 0xc(%rdx), %r12d
movl 0x20(%rax), %r15d
cmpl %r15d, %esi
cmovll %esi, %r15d
cmpl %r15d, %r12d
jg 0x1b812
cmpl %r12d, %ecx
jge 0x1b792
movl $0x50, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x40(%rax)
movq %rax, (%rsp)
movq (%r13), %rdi
callq 0x273e0
movq (%rsp), %rcx
movq %rax, (%rcx)
movl 0xc(%r13), %ecx
movq (%rsp), %rax
movl %ecx, 0xc(%rax)
decl %r12d
movl %r12d, 0x20(%rax)
movb 0x8(%r13), %cl
movb %cl, 0x8(%rax)
movq (%rbx), %rcx
movq 0x8(%rbx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x1b9e4
movq (%rcx,%r14,8), %rcx
movq %rcx, 0x48(%rax)
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x1b78a
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x1b792
movq %rsp, %rdx
callq 0xb976
cmpl %r15d, 0x20(%r13)
jle 0x1b804
movl $0x50, %edi
callq 0x2180
movq %rax, %r12
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x40(%rax)
movq (%r13), %rdi
callq 0x273e0
movq %rax, (%r12)
incl %r15d
movl %r15d, 0xc(%r12)
movl 0x20(%r13), %eax
movl %eax, 0x20(%r12)
movb 0x8(%r13), %al
movb %al, 0x8(%r12)
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x1b9e4
movq (%rax,%r14,8), %rax
movq %rax, 0x48(%r12)
jmp 0x1b807
xorl %r12d, %r12d
movq %r13, %rdi
callq 0x1bdea
movq %r12, %r13
movq 0x18(%rsp), %r15
incq %rbp
movq (%r15), %rcx
movq 0x8(%r15), %rax
subq %rcx, %rax
movq %rax, %rdx
sarq $0x3, %rdx
shrq $0x3, %rax
cltq
cmpq %rax, %rbp
jl 0x1b696
testq %r13, %r13
je 0x1b883
movq %r13, (%rsp)
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x1b9e4
movq (%rax,%r14,8), %rax
movq %rax, 0x48(%r13)
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x1b87b
movq %r13, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x1b883
movq %rsp, %rdx
callq 0xb976
incq %r14
movq (%rbx), %rcx
movq 0x8(%rbx), %rax
subq %rcx, %rax
movq %rax, %rdx
sarq $0x3, %rdx
shrq $0x3, %rax
cltq
cmpq %rax, %r14
jl 0x1b5fe
jmp 0x1b916
movq 0x8(%rbx), %rdx
movq %rdx, %rax
subq (%rbx), %rax
shrq $0x3, %rax
testl %eax, %eax
jle 0x1b916
xorl %r14d, %r14d
movq (%rbx), %rax
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x1b9e4
movq (%rax,%r14,8), %rdi
callq 0x172ac
movq %rax, (%rsp)
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x1b8f6
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x1b8fe
movq %rsp, %rdx
callq 0xb976
movq 0x8(%rbx), %rdx
movq %rdx, %rax
subq (%rbx), %rax
incq %r14
shrq $0x3, %rax
cltq
cmpq %rax, %r14
jl 0x1b8bd
movq 0x30(%rsp), %rsi
incq %rsi
movq 0x28(%rsp), %r14
movq 0x18(%r14), %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, %rsi
jl 0x1b594
movq %r14, %rdi
callq 0x16c1c
movq 0x10(%rsp), %rdi
callq 0x16c1c
movq 0x20(%rsp), %rdi
callq 0x1acd9
jmp 0x1b9d0
xorl %eax, %eax
jmp 0x1b9d5
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
subq %rsi, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x1b9d5
xorl %r15d, %r15d
movq %rsp, %r12
cmpq %r15, %rdx
jbe 0x1ba19
movq (%rsi,%r15,8), %rdi
callq 0x172ac
movq %rax, (%rsp)
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x10b7e
incq %r15
movq (%r14), %rsi
movq 0x8(%r14), %rcx
subq %rsi, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r15
jl 0x1b98d
movq 0x8(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11eeb(%rip), %rdi     # 0x2d8d6
movq %r14, %rsi
jmp 0x1ba12
leaq 0x11edf(%rip), %rdi     # 0x2d8d6
jmp 0x1ba12
leaq 0x11ed6(%rip), %rdi     # 0x2d8d6
movq %r14, %rsi
movq %rcx, %rdx
jmp 0x1ba12
leaq 0x11ec7(%rip), %rdi     # 0x2d8d6
movq %rbp, %rsi
xorl %eax, %eax
callq 0x2270
leaq 0x11eb6(%rip), %rdi     # 0x2d8d6
movq %r15, %rsi
jmp 0x1ba12