// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
testq %rdi, %rdi
je 0x1703d
movq %rsi, %rbx
movq %rdi, %r14
pushq $0x18
popq %rdi
callq 0x2170
movq %rax, %r12
testq %rbx, %rbx
je 0x17052
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
andq $0x0, 0x10(%r12)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1709e
movq %rax, %rbx
movq %r14, %rdi
callq 0x12b99
movq %rax, 0x38(%rsp)
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
callq 0x12b99
xorl %esi, %esi
movq %rax, %rbx
movq %rax, 0x30(%rsp)
movq %r12, 0x20(%rsp)
movq 0x38(%rsp), %rcx
movq 0x18(%rcx), %rdi
movq 0x8(%rdi), %rax
subq (%rdi), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rsi
jge 0x17021
movq (%rcx), %rax
movq (%rax,%rsi,8), %r13
movq %rbx, %r14
movq 0x18(%rbx), %rbx
movq %rsi, 0x40(%rsp)
callq 0x7342
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x21152
movq 0x8(%rbx), %rcx
subq (%rbx), %rcx
shrq $0x3, %rcx
cmpl %ecx, %eax
jge 0x16fd3
movq (%r14), %rcx
cltq
movq (%rcx,%rax,8), %r14
xorl %ebx, %ebx
movq %r13, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbx
jge 0x1700f
movq %r13, %rdi
movq %rbx, 0x18(%rsp)
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rdi
callq 0x134b2
movq %rax, %r15
movl 0xc(%rax), %edi
movq 0x8(%r14), %rcx
subq (%r14), %rcx
shrq $0x3, %rcx
decl %ecx
movq %r14, %rsi
xorl %edx, %edx
callq 0x12dee
movl %eax, %ebp
movl %eax, %ebx
movl %eax, %r14d
sarl $0x1f, %r14d
andl %eax, %r14d
incl %ebp
testl %ebx, %ebx
jle 0x16dfa
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %r12d
movq %r13, %rdi
movq 0x18(%rsp), %rsi
callq 0x7404
movq (%rax), %rax
decq %rbx
decl %ebp
cmpl 0xc(%rax), %r12d
jg 0x16dc5
jmp 0x16dfd
movl %r14d, %ebp
movslq %ebp, %rbp
movq 0x8(%rsp), %r14
movq 0x18(%rsp), %r12
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbp
jge 0x16f99
movq %r14, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %ebx
movq %r13, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
cmpl 0x20(%rax), %ebx
jg 0x16f99
testq %r15, %r15
je 0x16f99
leaq 0xc(%r15), %r12
movl 0xc(%r15), %ebx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rcx
movq %r12, %rax
cmpl 0xc(%rcx), %ebx
jg 0x16e81
movq %r14, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
addq $0xc, %rax
movl (%rax), %r13d
leaq 0x20(%r15), %rbx
movq %r14, %rdi
movl 0x20(%r15), %r14d
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rcx
movq %rbx, %rax
cmpl 0x20(%rcx), %r14d
jl 0x16eb7
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
addq $0x20, %rax
movl (%rax), %r14d
cmpl %r14d, %r13d
jle 0x16ed3
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %r14
movq 0x18(%rsp), %r12
jmp 0x16f91
cmpl %r13d, (%r12)
movq 0x18(%rsp), %r12
jge 0x16f34
callq 0x11e71
movq %rax, (%rsp)
movq (%r15), %rdi
callq 0x21062
movq (%rsp), %rcx
movq %rax, (%rcx)
movl 0xc(%r15), %eax
movq (%rsp), %rcx
movl %eax, 0xc(%rcx)
decl %r13d
movl %r13d, 0x20(%rcx)
movb 0x8(%r15), %al
movb %al, 0x8(%rcx)
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movq (%rsp), %rcx
movq %rax, 0x48(%rcx)
movq 0x20(%rsp), %rdi
movq %rsp, %rsi
callq 0x7448
cmpl %r14d, (%rbx)
jle 0x16f7a
callq 0x11e71
movq %rax, %rbx
movq (%r15), %rdi
callq 0x21062
movq %rax, (%rbx)
incl %r14d
movl %r14d, 0xc(%rbx)
movl 0x20(%r15), %eax
movl %eax, 0x20(%rbx)
movb 0x8(%r15), %al
movb %al, 0x8(%rbx)
movq 0x10(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movq %rax, 0x48(%rbx)
jmp 0x16f81
xorl %ebx, %ebx
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %r14
movq %r15, %rdi
callq 0x17375
movq %rbx, %r15
incq %rbp
jmp 0x16e0a
testq %r15, %r15
movq %r12, %rbx
movq 0x20(%rsp), %r12
je 0x16fcb
movq %r15, (%rsp)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
movq (%rsp), %rcx
movq %rax, 0x48(%rcx)
movq %r12, %rdi
movq %rsp, %rsi
callq 0x7448
incq %rbx
jmp 0x16d69
xorl %ebx, %ebx
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbx
jge 0x1700f
movq %r13, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rdi
callq 0x134b2
movq %rax, (%rsp)
movq %r12, %rdi
movq %rsp, %rsi
callq 0x7448
incq %rbx
jmp 0x16fd5
movq 0x40(%rsp), %rsi
incq %rsi
movq 0x30(%rsp), %rbx
jmp 0x16cff
movq %rcx, %rdi
callq 0x12f32
movq %rbx, %rdi
callq 0x12f32
movq 0x28(%rsp), %rdi
callq 0x165a8
jmp 0x17040
xorl %r12d, %r12d
movq %r12, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
andq $0x0, 0x10(%r12)
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
xorl %ebx, %ebx
movq %rsp, %r15
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbx
jge 0x17040
movq %r14, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rdi
callq 0x134b2
movq %rax, (%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0xd00a
incq %rbx
jmp 0x17065