// File: /workspace/github_cpp_makefile_star2_O3/gersteinlab[P]MUSIC/src/ms_annot_region_tools.cpp
// Function: merge_annot_regions(std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, int, bool)
vector<t_annot_region*>* merge_annot_regions(vector<t_annot_region*>* total_annot_regions,
											int max_gap, 
											bool match_strands) // Maximum distance between two annotated regions that are in the merged region.
{
	//printf("Merging %d regions.\n", total_annot_regions->size());

	//fprintf(stderr, "All region list contains %d regions.\n", total_annot_regions->size());

	vector<t_annot_region*>* merged_annot_regions = NULL;

	// Merged annot. regions.
	if(match_strands)
	{
		//printf("Merging with strand information.");
		// Merge plus strand regions.
		vector<t_annot_region*>* plus_strand_regions = get_regions_per_strand(total_annot_regions,
																			'+');
		//printf("%d plus strand regions.\n", plus_strand_regions->size());
		vector<t_annot_region*>* merged_plus_strand_regions = merge_annot_regions(plus_strand_regions, max_gap);

		// Merge negative strand regions.
		vector<t_annot_region*>* neg_strand_regions = get_regions_per_strand(total_annot_regions,
																			'-');
		//printf("%d negative strand regions.\n", neg_strand_regions->size());
		vector<t_annot_region*>* merged_neg_strand_regions = merge_annot_regions(neg_strand_regions, max_gap);

		// Pool the merged plus and minus strand regions.
		merged_annot_regions = new vector<t_annot_region*>();
		for(int i_reg = 0; i_reg < (int)merged_plus_strand_regions->size(); i_reg++)
		{
			merged_annot_regions->push_back(merged_plus_strand_regions->at(i_reg));
		} // i_reg loop

		for(int i_reg = 0; i_reg < (int)merged_neg_strand_regions->size(); i_reg++)
		{
			merged_annot_regions->push_back(merged_neg_strand_regions->at(i_reg));
		} // i_reg loop

		merged_plus_strand_regions->clear();
		delete(merged_plus_strand_regions);

		merged_neg_strand_regions->clear();
		delete(merged_neg_strand_regions);
	}
	else
	{
		 merged_annot_regions = merge_annot_regions(total_annot_regions, max_gap);
	}

	//printf("Merged into %d regions\n", merged_annot_regions->size());
	return(merged_annot_regions);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl %esi, %ebx
movq %rdi, %r14
testl %edx, %edx
je 0x1a854
movq %r14, %rdi
movl $0x2b, %esi
callq 0x1a265
movq %rax, %rdi
movl %ebx, %esi
callq 0x1b5ff
movq %rax, %r15
movq %r14, %rdi
movl $0x2d, %esi
callq 0x1a265
movq %rax, %rdi
movl %ebx, %esi
callq 0x1b5ff
movq %rax, %rbx
movl $0x18, %edi
callq 0x2180
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq (%r15), %rdx
movq 0x8(%r15), %rsi
movq %rsi, %rax
subq %rdx, %rax
sarq $0x3, %rax
testl %eax, %eax
jle 0x1a781
xorl %r13d, %r13d
xorl %r12d, %r12d
cmpq %r12, %rax
jbe 0x1a867
addq %r13, %rdx
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
je 0x1a752
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%r14)
jmp 0x1a75a
movq %r14, %rdi
callq 0xb74c
incq %r12
movq (%r15), %rdx
movq 0x8(%r15), %rsi
movq %rsi, %rcx
subq %rdx, %rcx
movq %rcx, %rax
sarq $0x3, %rax
shrq $0x3, %rcx
movslq %ecx, %rcx
addq $0x8, %r13
cmpq %rcx, %r12
jl 0x1a72f
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
testl %ecx, %ecx
jle 0x1a7f2
xorl %r13d, %r13d
xorl %r12d, %r12d
cmpq %r12, %rcx
jbe 0x1a876
addq %r13, %rax
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
je 0x1a7bc
movq (%rax), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%r14)
jmp 0x1a7c7
movq %r14, %rdi
movq %rax, %rdx
callq 0xb74c
incq %r12
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
movq %rdx, %rcx
sarq $0x3, %rcx
shrq $0x3, %rdx
movslq %edx, %rdx
addq $0x8, %r13
cmpq %rdx, %r12
jl 0x1a799
movq (%r15), %rdx
movq 0x8(%r15), %rsi
cmpq %rdx, %rsi
je 0x1a7fb
movq %rdx, 0x8(%r15)
testq %rdx, %rdx
je 0x1a80f
movq 0x10(%r15), %rsi
subq %rdx, %rsi
movq %rdx, %rdi
callq 0x2190
movl $0x18, %esi
movq %r15, %rdi
callq 0x2190
movq (%rbx), %rdi
cmpq %rdi, 0x8(%rbx)
je 0x1a829
movq %rdi, 0x8(%rbx)
testq %rdi, %rdi
je 0x1a83a
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x2190
movl $0x18, %esi
movq %rbx, %rdi
callq 0x2190
movq %r14, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq %r14, %rdi
movl %ebx, %esi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x1b5ff
leaq 0x12062(%rip), %rdi     # 0x2c8d0
movq %r12, %rsi
movq %rax, %rdx
jmp 0x1a883
leaq 0x12053(%rip), %rdi     # 0x2c8d0
movq %r12, %rsi
movq %rcx, %rdx
xorl %eax, %eax
callq 0x2270