// File: /workspace/github_cpp_makefile_star2_O3/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 0x1b531
movq %rsi, %r15
movq %rdi, %r14
movl $0x18, %edi
callq 0x2180
testq %r15, %r15
movq %rax, 0x8(%rsp)
xorps %xmm0, %xmm0
je 0x1b535
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %r15, %rdi
xorl %esi, %esi
callq 0x1b5ff
movq %rax, %rbx
movq %r14, %rdi
callq 0x163f1
movq %rax, %r12
movq %rbx, 0x18(%rsp)
movq %rbx, %rdi
callq 0x163f1
movq %rax, %r13
movq 0x18(%r12), %r15
movq (%r15), %rax
movq 0x8(%r15), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x1b515
xorl %esi, %esi
movq %r12, 0x28(%rsp)
movq %r13, 0x20(%rsp)
cmpq %rsi, %rdx
jbe 0x1b5bb
movq (%r12), %rcx
movq (%rcx,%rsi,8), %rbx
movq 0x18(%r13), %r14
movq %rsi, 0x30(%rsp)
movq (%rax,%rsi,8), %rsi
movq %r14, %rdi
callq 0x26ed0
movq 0x8(%r14), %rcx
subq (%r14), %rcx
shrq $0x3, %rcx
cmpl %ecx, %eax
jge 0x1b47a
movq (%rbx), %rcx
movq 0x8(%rbx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x1b4ec
movq (%r13), %rsi
cltq
movq (%rsi,%rax,8), %rax
movq %rax, 0x10(%rsp)
xorl %r14d, %r14d
cmpq %r14, %rdx
jbe 0x1b5af
movq (%rcx,%r14,8), %rdi
callq 0x16f1f
movq %rax, %r13
movl 0xc(%rax), %edi
movq 0x10(%rsp), %r15
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 0x1b24f
movl %eax, %esi
cmpq %rsi, %rdx
jbe 0x1b5bb
movq (%rbx), %r8
movq 0x8(%rbx), %rcx
subq %r8, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
jbe 0x1b5c4
movq (%rdi,%rsi,8), %rcx
movl 0x20(%rcx), %ecx
movq (%r8,%r14,8), %rsi
cmpl 0xc(%rsi), %ecx
jle 0x1b24f
leal -0x1(%rax), %ecx
cmpl $0x1, %eax
movl %ecx, %eax
jg 0x1b211
xorl %eax, %eax
cmpl %edx, %eax
jge 0x1b402
movslq %eax, %rbp
cmpq %rbp, %rdx
jbe 0x1b5d3
movq (%rbx), %rcx
movq 0x8(%rbx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x1b5af
movq (%rdi,%rbp,8), %rdx
movl 0xc(%rdx), %eax
movq (%rcx,%r14,8), %rcx
cmpl 0x20(%rcx), %eax
jg 0x1b3f7
testq %r13, %r13
je 0x1b3f7
movl 0xc(%r13), %esi
movl 0x20(%r13), %ecx
cmpl %eax, %esi
movl %eax, %r15d
cmovgl %esi, %r15d
movl 0x20(%rdx), %r12d
cmpl %r12d, %ecx
cmovll %ecx, %r12d
cmpl %r12d, %r15d
jg 0x1b3cf
cmpl %esi, %eax
jle 0x1b355
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 0x26db4
movq (%rsp), %rcx
movq %rax, (%rcx)
movl 0xc(%r13), %ecx
movq (%rsp), %rax
movl %ecx, 0xc(%rax)
decl %r15d
movl %r15d, 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 0x1b5af
movq (%rcx,%r14,8), %rcx
movq %rcx, 0x48(%rax)
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x1b349
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x1b351
movq %rsp, %rdx
callq 0xb74c
movl 0x20(%r13), %ecx
cmpl %r12d, %ecx
jle 0x1b3c1
movl $0x50, %edi
callq 0x2180
movq %rax, %r15
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 0x26db4
movq %rax, (%r15)
incl %r12d
movl %r12d, 0xc(%r15)
movl 0x20(%r13), %eax
movl %eax, 0x20(%r15)
movb 0x8(%r13), %al
movb %al, 0x8(%r15)
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x1b5af
movq (%rax,%r14,8), %rax
movq %rax, 0x48(%r15)
jmp 0x1b3c4
xorl %r15d, %r15d
movq %r13, %rdi
callq 0x1b9d5
movq %r15, %r13
incq %rbp
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rax
subq %rdi, %rax
movq %rax, %rdx
sarq $0x3, %rdx
shrq $0x3, %rax
cltq
cmpq %rax, %rbp
jl 0x1b25a
movq 0x8(%rbx), %rax
testq %r13, %r13
jne 0x1b406
jmp 0x1b44a
movq 0x8(%rbx), %rax
movq %r13, (%rsp)
movq (%rbx), %rcx
subq %rcx, %rax
sarq $0x3, %rax
cmpq %r14, %rax
jbe 0x1b5e4
movq (%rcx,%r14,8), %rax
movq %rax, 0x48(%r13)
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x1b43e
movq %r13, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x1b446
movq %rsp, %rdx
callq 0xb74c
movq 0x8(%rbx), %rax
incq %r14
movq (%rbx), %rcx
subq %rcx, %rax
movq %rax, %rdx
sarq $0x3, %rdx
shrq $0x3, %rax
cltq
cmpq %rax, %r14
jl 0x1b1cb
movq 0x28(%rsp), %r12
movq 0x18(%r12), %r15
movq 0x20(%rsp), %r13
jmp 0x1b4ec
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movq %rdx, %rcx
subq %rax, %rcx
shrq $0x3, %rcx
testl %ecx, %ecx
jle 0x1b4ec
xorl %r14d, %r14d
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x1b5af
movq (%rax,%r14,8), %rdi
callq 0x16f1f
movq %rax, (%rsp)
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x1b4c8
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x1b4d0
movq %rsp, %rdx
callq 0xb74c
incq %r14
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movq %rdx, %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r14
jl 0x1b492
movq 0x30(%rsp), %rsi
incq %rsi
movq (%r15), %rax
movq 0x8(%r15), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rsi
jl 0x1b16a
movq %r12, %rdi
callq 0x16877
movq %r13, %rdi
callq 0x16877
movq 0x18(%rsp), %rdi
callq 0x1a88a
jmp 0x1b59b
xorl %eax, %eax
jmp 0x1b5a0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x1b59b
xorl %r15d, %r15d
movq %rsp, %r12
cmpq %r15, %rdx
jbe 0x1b5f3
movq (%rax,%r15,8), %rdi
callq 0x16f1f
movq %rax, (%rsp)
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x108ac
incq %r15
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r15
jl 0x1b558
movq 0x8(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1131a(%rip), %rdi     # 0x2c8d0
movq %r14, %rsi
jmp 0x1b5dd
leaq 0x1130e(%rip), %rdi     # 0x2c8d0
jmp 0x1b5dd
leaq 0x11305(%rip), %rdi     # 0x2c8d0
movq %r14, %rsi
movq %rcx, %rdx
jmp 0x1b5dd
leaq 0x112f6(%rip), %rdi     # 0x2c8d0
movq %rbp, %rsi
xorl %eax, %eax
callq 0x2270
leaq 0x112e5(%rip), %rdi     # 0x2c8d0
movq %r14, %rsi
movq %rax, %rdx
jmp 0x1b5dd
leaq 0x112d6(%rip), %rdi     # 0x2c8d0
movq %r15, %rsi
jmp 0x1b5dd