// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %ebp
movq %rsi, %r13
movq %rdi, %r14
movl $0x18, %edi
callq 0x2180
movq %rax, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
testl %ebp, %ebp
jne 0x1a449
movb 0x8(%r14), %al
cmpb 0x8(%r13), %al
jne 0x1a4b9
movq (%r14), %r12
movq (%r13), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x2240
testl %eax, %eax
jne 0x1a4b9
movl 0xc(%r14), %ecx
movl 0x20(%r14), %r8d
movl 0xc(%r13), %r10d
movl 0x20(%r13), %r11d
cmpl %r10d, %ecx
movl %r10d, %ebp
cmovgl %ecx, %ebp
cmpl %r11d, %r8d
movl %r11d, %r13d
cmovll %r8d, %r13d
cmpl %r13d, %ebp
jge 0x1a4b9
cmpl %r10d, %ecx
setge %al
cmpl %r11d, %r8d
setle %dl
testb %dl, %al
jne 0x1a4e7
cmpl %ecx, %r10d
setle %al
cmpl %r11d, %r8d
setg %dl
orb %al, %dl
jne 0x1a4f9
movq %r14, %rdi
callq 0x172ac
movq %rax, 0x8(%rsp)
movl %ebp, 0x20(%rax)
jmp 0x1a4c6
movq %r14, %rdi
callq 0x172ac
movq %rax, 0x8(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x1a4da
movq %rax, (%rsi)
addq $0x8, 0x8(%rbx)
jmp 0x1a4e7
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0xb976
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl %r10d, %ecx
setl %al
cmpl %r8d, %r11d
setge %dl
orb %al, %dl
jne 0x1a527
movq %r14, %rdi
callq 0x172ac
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
movl %r13d, 0xc(%rax)
movq %rbx, %rdi
callq 0x1c7fc
jmp 0x1a4e7
cmpl %ecx, %r10d
jle 0x1a596
cmpl %r8d, %r11d
jge 0x1a596
movq %r14, %rdi
callq 0x172ac
movq %rax, 0x8(%rsp)
movq %r14, %rdi
callq 0x172ac
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rcx
movl %ebp, 0x20(%rcx)
movl %r13d, 0xc(%rax)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x1a56b
movq %rcx, (%rsi)
addq $0x8, 0x8(%rbx)
jmp 0x1a578
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0xb976
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x1a58c
movq 0x10(%rsp), %rax
jmp 0x1a4d0
leaq 0x10(%rsp), %rdx
jmp 0x1a4df
movq 0x1fa43(%rip), %rax     # 0x39fe0
movq (%rax), %rdi
leaq 0x1481b(%rip), %rsi     # 0x2edc2
movq %r12, %rdx
movq %r15, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x2250
addq $0x10, %rsp
xorl %edi, %edi
callq 0x21e0