// File: /workspace/github_cpp_makefile_star2_O1/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 0x1acb2
movq %r14, %rdi
movl $0x2b, %esi
callq 0x1a69a
movq %rax, %rdi
movl %ebx, %esi
callq 0x1ba25
movq %rax, %r15
movq %r14, %rdi
movl $0x2d, %esi
callq 0x1a69a
movq %rax, %rdi
movl %ebx, %esi
callq 0x1ba25
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), %rax
subq %rdx, %rax
sarq $0x3, %rax
testl %eax, %eax
jle 0x1abe8
xorl %r13d, %r13d
xorl %r12d, %r12d
cmpq %r12, %rax
jbe 0x1acc5
addq %r13, %rdx
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
je 0x1abbc
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%r14)
jmp 0x1abc4
movq %r14, %rdi
callq 0xb976
incq %r12
movq (%r15), %rdx
movq 0x8(%r15), %rcx
subq %rdx, %rcx
movq %rcx, %rax
sarq $0x3, %rax
shrq $0x3, %rcx
movslq %ecx, %rcx
addq $0x8, %r13
cmpq %rcx, %r12
jl 0x1ab99
movq (%rbx), %rdx
movq 0x8(%rbx), %rax
subq %rdx, %rax
sarq $0x3, %rax
testl %eax, %eax
jle 0x1ac4f
xorl %r13d, %r13d
xorl %r12d, %r12d
cmpq %r12, %rax
jbe 0x1acc5
addq %r13, %rdx
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
je 0x1ac23
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%r14)
jmp 0x1ac2b
movq %r14, %rdi
callq 0xb976
incq %r12
movq (%rbx), %rdx
movq 0x8(%rbx), %rcx
subq %rdx, %rcx
movq %rcx, %rax
sarq $0x3, %rax
shrq $0x3, %rcx
movslq %ecx, %rcx
addq $0x8, %r13
cmpq %rcx, %r12
jl 0x1ac00
movq (%r15), %rdi
cmpq %rdi, 0x8(%r15)
je 0x1ac5c
movq %rdi, 0x8(%r15)
testq %rdi, %rdi
je 0x1ac6d
movq 0x10(%r15), %rsi
subq %rdi, %rsi
callq 0x2190
movl $0x18, %esi
movq %r15, %rdi
callq 0x2190
movq (%rbx), %rdi
cmpq %rdi, 0x8(%rbx)
je 0x1ac87
movq %rdi, 0x8(%rbx)
testq %rdi, %rdi
je 0x1ac98
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 0x1ba25
leaq 0x12c0a(%rip), %rdi     # 0x2d8d6
movq %r12, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2270