// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
pushq %rax
movl %esi, %ebx
movq %rdi, %r15
testl %edx, %edx
je 0x16593
pushq $0x2b
popq %rsi
movq %r15, %rdi
callq 0x16108
movq %rax, %rdi
movl %ebx, %esi
callq 0x1709e
movq %rax, %r14
pushq $0x2d
popq %rsi
movq %r15, %rdi
callq 0x16108
movq %rax, %rdi
movl %ebx, %esi
callq 0x1709e
movq %rax, %rbx
pushq $0x18
popq %rdi
callq 0x2170
movq %rax, %r15
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
xorl %r12d, %r12d
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r12
jge 0x16514
movq %r14, %rdi
movq %r12, %rsi
callq 0x7404
movq %r15, %rdi
movq %rax, %rsi
callq 0x7448
incq %r12
jmp 0x164e7
xorl %r12d, %r12d
movq 0x8(%rbx), %rax
subq (%rbx), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r12
jge 0x16544
movq %rbx, %rdi
movq %r12, %rsi
callq 0x7404
movq %r15, %rdi
movq %rax, %rsi
callq 0x7448
incq %r12
jmp 0x16517
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x16551
movq %rax, 0x8(%r14)
movq %r14, %rdi
callq 0x832c
pushq $0x18
popq %rsi
movq %r14, %rdi
callq 0x2180
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x16571
movq %rax, 0x8(%rbx)
movq %rbx, %rdi
callq 0x832c
pushq $0x18
popq %rsi
movq %rbx, %rdi
callq 0x2180
movq %r15, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq %r15, %rdi
movl %ebx, %esi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x1709e