// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x18
popq %rdi
callq 0x2170
movq %rax, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
testl %ebp, %ebp
jne 0x15ef5
movb 0x8(%r14), %al
cmpb 0x8(%r13), %al
jne 0x15f68
movq (%r14), %r12
movq (%r13), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x2230
testl %eax, %eax
jne 0x15f68
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 0x15f68
cmpl %r10d, %ecx
setge %al
cmpl %r11d, %r8d
setle %dl
testb %dl, %al
jne 0x15f80
cmpl %r11d, %r8d
setg %al
cmpl %ecx, %r10d
setle %dl
orb %al, %dl
jne 0x15f92
movq %r14, %rdi
callq 0x134b2
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
movl %ebp, 0x20(%rax)
jmp 0x15f78
movq %r14, %rdi
callq 0x134b2
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
movq %rbx, %rdi
callq 0x7448
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 0x15fb8
movq %r14, %rdi
callq 0x134b2
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
movl %r13d, 0xc(%rax)
jmp 0x15f78
cmpl %ecx, %r10d
jle 0x16004
cmpl %r8d, %r11d
jge 0x16004
movq %r14, %rdi
callq 0x134b2
movq %rax, %r15
leaq 0x8(%rsp), %r12
movq %rax, (%r12)
movq %r14, %rdi
callq 0x134b2
leaq 0x10(%rsp), %r14
movq %rax, (%r14)
movl %ebp, 0x20(%r15)
movl %r13d, 0xc(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x7448
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x15f7b
movq 0x1efd5(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0x11dad(%rip), %rsi     # 0x27dc2
movq %r12, %rdx
movq %r15, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x2240
popq %rcx
popq %rdx
xorl %edi, %edi
callq 0x21d0