// File: gersteinlab[P]MUSIC/src/ms_annot_region_tools.cpp
// Function: subtract_annot_regions(t_annot_region*, t_annot_region*, bool)
vector<t_annot_region*>* subtract_annot_regions(t_annot_region* region1, t_annot_region* region2, bool strand_specific)
{
	vector<t_annot_region*>* remaining_regions = new vector<t_annot_region*>();	

	if((!strand_specific && region1->strand != region2->strand) || 
		(strcmp(region1->chrom, region2->chrom) != 0))
	{
		t_annot_region* dup_region = duplicate_region(region1);
		remaining_regions->push_back(dup_region);
		return(remaining_regions);
	}

	// Check if there is overlap between region1 and region2.
	int ol_start = MAX(region1->start, region2->start);
	int ol_end = MIN(region1->end, region2->end);

	if(ol_start < ol_end)
	{
		// There is overlap between regions. Take the region that is excluded from the overlap. 
		// There are 4 possibilities of overlap:
		// os=s1<e1=ol: region2 fully contains region1
		// s1<os<e1=ol: region2 overlaps partialy with region1 on right side.
		// s1=os<ol<e1: region2 overlaps partialy with region1 on left side.
		// s1<os<ol<e1: region1 fully contains region2 with extra parts on the sides.
		if(region1->start == ol_start && 
			region1->end == ol_end)
		{
			// Nothing is left in region1 after exclusion.
		}
		else if(region1->start < ol_start && 
			region1->end == ol_end)
		{
			// Left side of region1 is left.
			t_annot_region* dup_region = duplicate_region(region1);
			dup_region->end = ol_start;
			remaining_regions->push_back(dup_region);
		}
		else if(region1->start == ol_start && 
			ol_end < region1->end)
		{
			t_annot_region* dup_region = duplicate_region(region1);
			dup_region->start = ol_end;
			remaining_regions->push_back(dup_region);
		}
		else if(region1->start < ol_start && 
			ol_end < region1->end)
		{
			t_annot_region* dup_region1 = duplicate_region(region1);
			t_annot_region* dup_region2 = duplicate_region(region1);
			dup_region1->end = ol_start;
			dup_region2->start = ol_end;
			remaining_regions->push_back(dup_region1);
			remaining_regions->push_back(dup_region2);
		}
		else
		{
			fprintf(stderr, "Could not resolve the excluded region with overlapping of: %s:%d-%d and %s:%d-%d.\n", region1->chrom, region1->start, region1->end,
				region2->chrom, region2->start, region2->end);
			exit(0);
		}
	}
	else
	{
		t_annot_region* dup_region = duplicate_region(region1);
		remaining_regions->push_back(dup_region);
	}

	return(remaining_regions);
}

// Assembly:
subq $0x88, %rsp
movb %dl, %al
movq %rdi, 0x78(%rsp)
movq %rsi, 0x70(%rsp)
andb $0x1, %al
movb %al, 0x6f(%rsp)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x20(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x20(%rsp), %rdi
callq 0xc670
movq 0x20(%rsp), %rax
movq %rax, 0x60(%rsp)
testb $0x1, 0x6f(%rsp)
jne 0x2e868
movq 0x78(%rsp), %rax
movsbl 0x8(%rax), %eax
movq 0x70(%rsp), %rcx
movsbl 0x8(%rcx), %ecx
cmpl %ecx, %eax
jne 0x2e882
movq 0x78(%rsp), %rax
movq (%rax), %rdi
movq 0x70(%rsp), %rax
movq (%rax), %rsi
callq 0x2240
cmpl $0x0, %eax
je 0x2e8b2
movq 0x78(%rsp), %rdi
callq 0x2a410
movq %rax, 0x58(%rsp)
movq 0x60(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0xc8c0
movq 0x60(%rsp), %rax
movq %rax, 0x80(%rsp)
jmp 0x2eadd
movq 0x78(%rsp), %rax
movl 0xc(%rax), %eax
movq 0x70(%rsp), %rcx
cmpl 0xc(%rcx), %eax
jle 0x2e8d2
movq 0x78(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x1c(%rsp)
jmp 0x2e8de
movq 0x70(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x54(%rsp)
movq 0x78(%rsp), %rax
movl 0x20(%rax), %eax
movq 0x70(%rsp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x2e906
movq 0x78(%rsp), %rax
movl 0x20(%rax), %eax
movl %eax, 0x18(%rsp)
jmp 0x2e912
movq 0x70(%rsp), %rax
movl 0x20(%rax), %eax
movl %eax, 0x18(%rsp)
movl 0x18(%rsp), %eax
movl %eax, 0x50(%rsp)
movl 0x54(%rsp), %eax
cmpl 0x50(%rsp), %eax
jge 0x2eab2
movq 0x78(%rsp), %rax
movl 0xc(%rax), %eax
cmpl 0x54(%rsp), %eax
jne 0x2e949
movq 0x78(%rsp), %rax
movl 0x20(%rax), %eax
cmpl 0x50(%rsp), %eax
jne 0x2e949
jmp 0x2eab0
movq 0x78(%rsp), %rax
movl 0xc(%rax), %eax
cmpl 0x54(%rsp), %eax
jge 0x2e994
movq 0x78(%rsp), %rax
movl 0x20(%rax), %eax
cmpl 0x50(%rsp), %eax
jne 0x2e994
movq 0x78(%rsp), %rdi
callq 0x2a410
movq %rax, 0x48(%rsp)
movl 0x54(%rsp), %ecx
movq 0x48(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x60(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0xc8c0
jmp 0x2eaae
movq 0x78(%rsp), %rax
movl 0xc(%rax), %eax
cmpl 0x54(%rsp), %eax
jne 0x2e9df
movl 0x50(%rsp), %eax
movq 0x78(%rsp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x2e9df
movq 0x78(%rsp), %rdi
callq 0x2a410
movq %rax, 0x40(%rsp)
movl 0x50(%rsp), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0xc(%rax)
movq 0x60(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0xc8c0
jmp 0x2eaac
movq 0x78(%rsp), %rax
movl 0xc(%rax), %eax
cmpl 0x54(%rsp), %eax
jge 0x2ea51
movl 0x50(%rsp), %eax
movq 0x78(%rsp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x2ea51
movq 0x78(%rsp), %rdi
callq 0x2a410
movq %rax, 0x38(%rsp)
movq 0x78(%rsp), %rdi
callq 0x2a410
movq %rax, 0x30(%rsp)
movl 0x54(%rsp), %ecx
movq 0x38(%rsp), %rax
movl %ecx, 0x20(%rax)
movl 0x50(%rsp), %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0xc(%rax)
movq 0x60(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0xc8c0
movq 0x60(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0xc8c0
jmp 0x2eaaa
movq 0x36588(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movq 0x78(%rsp), %rax
movq (%rax), %rdx
movq 0x78(%rsp), %rax
movl 0xc(%rax), %ecx
movq 0x78(%rsp), %rax
movl 0x20(%rax), %r8d
movq 0x70(%rsp), %rax
movq (%rax), %r9
movq 0x70(%rsp), %rax
movl 0xc(%rax), %r10d
movq 0x70(%rsp), %rax
movl 0x20(%rax), %eax
leaq 0x27736(%rip), %rsi     # 0x561ca
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
jmp 0x2eaac
jmp 0x2eaae
jmp 0x2eab0
jmp 0x2ead0
movq 0x78(%rsp), %rdi
callq 0x2a410
movq %rax, 0x28(%rsp)
movq 0x60(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0xc8c0
movq 0x60(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rax
addq $0x88, %rsp
retq
nopl (%rax)