// File: 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:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x118(%rsp)
movq %rsi, 0x110(%rsp)
cmpq $0x0, 0x118(%rsp)
jne 0x30143
movq $0x0, 0x120(%rsp)
jmp 0x308de
cmpq $0x0, 0x110(%rsp)
jne 0x30221
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x68(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x68(%rsp), %rdi
callq 0xc670
movq 0x68(%rsp), %rax
movq %rax, 0x108(%rsp)
movl $0x0, 0x104(%rsp)
movl 0x104(%rsp), %eax
movl %eax, 0x64(%rsp)
movq 0x118(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0x64(%rsp), %eax
cmpl %ecx, %eax
jge 0x3020c
movq 0x108(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x118(%rsp), %rdi
movslq 0x104(%rsp), %rsi
callq 0xc800
movq (%rax), %rdi
callq 0x2a410
movq 0x58(%rsp), %rdi
movq %rax, 0xf8(%rsp)
leaq 0xf8(%rsp), %rsi
callq 0x1acc0
movl 0x104(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x104(%rsp)
jmp 0x30192
movq 0x108(%rsp), %rax
movq %rax, 0x120(%rsp)
jmp 0x308de
jmp 0x30223
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x50(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x50(%rsp), %rdi
callq 0xc670
movq 0x50(%rsp), %rax
movq %rax, 0xf0(%rsp)
movq 0x110(%rsp), %rdi
xorl %esi, %esi
callq 0x308f0
movq %rax, 0xe8(%rsp)
movq 0x118(%rsp), %rdi
callq 0x29200
movq %rax, 0xe0(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x29200
movq %rax, 0xd8(%rsp)
movl $0x0, 0xd4(%rsp)
movl 0xd4(%rsp), %eax
movl %eax, 0x4c(%rsp)
movq 0xe0(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x4c(%rsp), %eax
cmpl %ecx, %eax
jge 0x308a7
movq 0xe0(%rsp), %rax
movq (%rax), %rax
movslq 0xd4(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0xc8(%rsp)
movq 0xd8(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x40(%rsp)
movq 0xe0(%rsp), %rax
movq 0x18(%rax), %rdi
movslq 0xd4(%rsp), %rsi
callq 0xc240
movq 0x40(%rsp), %rdi
movq (%rax), %rsi
callq 0x47cf0
movl %eax, 0xc4(%rsp)
movl 0xc4(%rsp), %eax
movl %eax, 0x48(%rsp)
movq 0xd8(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x48(%rsp), %eax
cmpl %ecx, %eax
jge 0x30827
movq 0xd8(%rsp), %rax
movq (%rax), %rax
movslq 0xc4(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0xb8(%rsp)
movl $0x0, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq 0xc8(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0x3c(%rsp), %eax
cmpl %ecx, %eax
jge 0x30825
movq 0xc8(%rsp), %rdi
movslq 0xb4(%rsp), %rsi
callq 0xc800
movq (%rax), %rdi
callq 0x2a410
movq %rax, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x2c(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0xb8(%rsp), %rdi
callq 0xc680
movl 0x2c(%rsp), %edi
movq 0x30(%rsp), %rsi
movl %eax, %ecx
subl $0x1, %ecx
xorl %edx, %edx
callq 0x297a0
movl %eax, 0xa4(%rsp)
xorl %eax, %eax
cmpl $0x0, 0xa4(%rsp)
movb %al, 0x2b(%rsp)
jle 0x3046a
movq 0xb8(%rsp), %rdi
movslq 0xa4(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x24(%rsp)
movq 0xc8(%rsp), %rdi
movslq 0xb4(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x24(%rsp), %eax
movq (%rcx), %rcx
cmpl 0xc(%rcx), %eax
setg %al
movb %al, 0x2b(%rsp)
movb 0x2b(%rsp), %al
testb $0x1, %al
jne 0x30474
jmp 0x30487
movl 0xa4(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x30412
jmp 0x30489
movl 0xa4(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0xb8(%rsp), %rdi
callq 0xc680
movl 0x1c(%rsp), %ecx
movl %eax, %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, 0x23(%rsp)
jge 0x30507
movq 0xb8(%rsp), %rdi
movslq 0xa4(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x18(%rsp)
movq 0xc8(%rsp), %rdi
movslq 0xb4(%rsp), %rsi
callq 0xc800
movl 0x18(%rsp), %ecx
movq (%rax), %rdx
xorl %eax, %eax
cmpl 0x20(%rdx), %ecx
movb %al, 0x23(%rsp)
jg 0x30507
cmpq $0x0, 0xa8(%rsp)
setne %al
movb %al, 0x23(%rsp)
movb 0x23(%rsp), %al
testb $0x1, %al
jne 0x30514
jmp 0x307b9
movq 0xa8(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x14(%rsp)
movq 0xb8(%rsp), %rdi
movslq 0xa4(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x14(%rsp), %eax
movq (%rcx), %rcx
cmpl 0xc(%rcx), %eax
jle 0x30558
movq 0xa8(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x10(%rsp)
jmp 0x30577
movq 0xb8(%rsp), %rdi
movslq 0xa4(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0xa0(%rsp)
movq 0xa8(%rsp), %rax
movl 0x20(%rax), %eax
movl %eax, 0xc(%rsp)
movq 0xb8(%rsp), %rdi
movslq 0xa4(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0xc(%rsp), %eax
movq (%rcx), %rcx
cmpl 0x20(%rcx), %eax
jge 0x305c6
movq 0xa8(%rsp), %rax
movl 0x20(%rax), %eax
movl %eax, 0x8(%rsp)
jmp 0x305e5
movq 0xb8(%rsp), %rdi
movslq 0xa4(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0x9c(%rsp)
movl 0xa0(%rsp), %eax
cmpl 0x9c(%rsp), %eax
jg 0x307a3
movq 0xa8(%rsp), %rax
movl 0xc(%rax), %eax
cmpl 0xa0(%rsp), %eax
jge 0x306c1
callq 0x27b00
movq %rax, 0x90(%rsp)
movq 0xa8(%rsp), %rax
movq (%rax), %rdi
callq 0x47b60
movq %rax, %rcx
movq 0x90(%rsp), %rax
movq %rcx, (%rax)
movq 0xa8(%rsp), %rax
movl 0xc(%rax), %ecx
movq 0x90(%rsp), %rax
movl %ecx, 0xc(%rax)
movl 0xa0(%rsp), %ecx
subl $0x1, %ecx
movq 0x90(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0xa8(%rsp), %rax
movb 0x8(%rax), %cl
movq 0x90(%rsp), %rax
movb %cl, 0x8(%rax)
movq 0xc8(%rsp), %rdi
movslq 0xb4(%rsp), %rsi
callq 0xc800
movq (%rax), %rcx
movq 0x90(%rsp), %rax
movq %rcx, 0x48(%rax)
movq 0xf0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0xc8c0
movq 0xa8(%rsp), %rax
movl 0x20(%rax), %eax
cmpl 0x9c(%rsp), %eax
jle 0x30788
callq 0x27b00
movq %rax, 0x88(%rsp)
movq 0xa8(%rsp), %rax
movq (%rax), %rdi
callq 0x47b60
movq %rax, %rcx
movq 0x88(%rsp), %rax
movq %rcx, (%rax)
movl 0x9c(%rsp), %ecx
addl $0x1, %ecx
movq 0x88(%rsp), %rax
movl %ecx, 0xc(%rax)
movq 0xa8(%rsp), %rax
movl 0x20(%rax), %ecx
movq 0x88(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0xa8(%rsp), %rax
movb 0x8(%rax), %cl
movq 0x88(%rsp), %rax
movb %cl, 0x8(%rax)
movq 0xc8(%rsp), %rdi
movslq 0xb4(%rsp), %rsi
callq 0xc800
movq (%rax), %rcx
movq 0x88(%rsp), %rax
movq %rcx, 0x48(%rax)
movq 0xa8(%rsp), %rdi
callq 0x30dc0
movq 0x88(%rsp), %rax
movq %rax, 0xa8(%rsp)
jmp 0x307a1
movq 0xa8(%rsp), %rdi
callq 0x30dc0
movq $0x0, 0xa8(%rsp)
jmp 0x307a3
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x30489
cmpq $0x0, 0xa8(%rsp)
je 0x3080d
movq 0xa8(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0xc8(%rsp), %rdi
movslq 0xb4(%rsp), %rsi
callq 0xc800
movq (%rax), %rcx
movq 0x80(%rsp), %rax
movq %rcx, 0x48(%rax)
movq 0xf0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xc8c0
jmp 0x3080f
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x30381
jmp 0x3088f
movl $0x0, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
movl %eax, 0x4(%rsp)
movq 0xc8(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0x4(%rsp), %eax
cmpl %ecx, %eax
jge 0x3088d
movq 0xc8(%rsp), %rdi
movslq 0x7c(%rsp), %rsi
callq 0xc800
movq (%rax), %rdi
callq 0x2a410
movq %rax, 0x70(%rsp)
movq 0xf0(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0xc8c0
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x3082f
jmp 0x3088f
jmp 0x30891
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x302a4
movq 0xe0(%rsp), %rdi
callq 0x29990
movq 0xd8(%rsp), %rdi
callq 0x29990
movq 0xe8(%rsp), %rdi
callq 0x2f560
movq 0xf0(%rsp), %rax
movq %rax, 0x120(%rsp)
movq 0x120(%rsp), %rax
addq $0x128, %rsp            # imm = 0x128
retq
nop