// 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)
vector<t_annot_region*>* merge_annot_regions(vector<t_annot_region*>* total_annot_regions,
											int max_gap) // Maximum distance between two annotated regions that are in the merged region.
{
	//printf("Merging %d regions.\n", total_annot_regions->size());

	// Determine the chromosome ids from the total region list.
	vector<char*>* chr_ids = get_chr_ids(total_annot_regions);
	//printf("Found %d different chromosomes in regions list.\n", chr_ids->size());

	//fprintf(stderr, "All region list contains %d regions.\n", total_annot_regions->size());

	// Merged annot. regions.
	vector<t_annot_region*>* merged_annot_regions = new vector<t_annot_region*>();

	// Go over all the regions in total_annot_regions and merge necessary ones.
	for(int i_chr = 0; i_chr < (int)chr_ids->size(); i_chr++)
	{
		vector<t_annot_region*>* cur_chr_regions = get_regions_per_chromosome(total_annot_regions,
																				chr_ids->at(i_chr));

        sort(cur_chr_regions->begin(), cur_chr_regions->end(), sort_regions);
			
		if(cur_chr_regions->size() > 0)
		{
			//printf("Loaded %d regions in %s\n", cur_chr_regions->size(), chr_ids->at(i_chr));

			int i = 0;
			int cur_start = cur_chr_regions->at(i)->start;
			int cur_end = cur_chr_regions->at(i)->end;

			// Find the next read that is to be merged in this chromosome.
			while(i < (int)cur_chr_regions->size())
			{
				// Is this not the last region in the region list?
				if(i+1 < (int)cur_chr_regions->size())
				{
					// Check max_gap condition for merging: Start of the next region should be at most max_gap nucs away from end of current region.
					if(cur_chr_regions->at(i+1)->start <= (cur_end + max_gap))
					{
						//fprintf(stderr, "Merging: <%d-%d>, cur_end = %d\n", cur_chr_regions->at(i+1)->start, cur_chr_regions->at(i+1)->end, cur_end);
						// Update cur_end, if the end of next region is passing cur_end, update cur_end, otherwise this region is a subset of previous regions.
						cur_end = (cur_end < cur_chr_regions->at(i+1)->end)?(cur_chr_regions->at(i+1)->end):cur_end;
					}
					else
					{
						// Add the region that has accumulated so far: Note that the last region is processed in the previous iteration, it therefore is not 
						// necessary to process that last region any more.
						t_annot_region* new_region = new t_annot_region();
						new_region->start = cur_start;
						new_region->end = cur_end;
						new_region->name = NULL;
						new_region->chrom = new char[strlen(cur_chr_regions->at(i)->chrom) + 2];
						strcpy(new_region->chrom, cur_chr_regions->at(i)->chrom);
						new_region->strand = cur_chr_regions->at(i)->strand;

						merged_annot_regions->push_back(new_region);
						//fprintf(stderr, "Adding merged region <%d-%d>.\n", merged_annot_regions->back()->start, merged_annot_regions->back()->end);

						// Set the current start to start of next region.
						cur_start = cur_chr_regions->at(i+1)->start;
						cur_end = cur_chr_regions->at(i+1)->end;
						//printf("Initiated a new region @ <%d-.>\n", cur_start);
					}
				}
				else
				{
					// This is the last region. Set the max to its ending and add the last merged region.
					t_annot_region* new_region = new t_annot_region();
					new_region->start = cur_start;
					//new_region->end = cur_chr_regions->at(i)->end;
					new_region->end = cur_end;
					new_region->name = NULL;
					new_region->chrom = new char[strlen(cur_chr_regions->at(i)->chrom) + 2];
					strcpy(new_region->chrom, cur_chr_regions->at(i)->chrom);
					new_region->strand = cur_chr_regions->at(i)->strand;

					merged_annot_regions->push_back(new_region);

					//fprintf(stderr, "Adding merged region <%d-%d>.\n", merged_annot_regions->back()->start, merged_annot_regions->back()->end);
					break;
				}

				i++;
			} // i loop 
		} // region size check to make sure that there are regions in this chromosome.

		// Delete the regions for current chromosome. And move to the next one.
		cur_chr_regions->clear();
		delete(cur_chr_regions);
	} // chr check.

	// Clean the chromosome ids.
	t_string::clean_string_list(chr_ids);

	//printf("Merged into %d regions\n", merged_annot_regions->size());
	return(merged_annot_regions);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, 0x30(%rsp)
movq %rdi, 0x28(%rsp)
callq 0x137a1
movq %rax, %rbx
pushq $0x18
popq %rdi
callq 0x2170
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x18(%rsp)
andq $0x0, 0x10(%rax)
xorl %ebp, %ebp
movq %rbx, 0x20(%rsp)
movq 0x8(%rbx), %rax
subq (%rbx), %rax
shrq $0x3, %rax
cltq
movq %rbx, %rdi
cmpq %rax, %rbp
jge 0x1735c
movq %rbp, %rsi
callq 0x7342
movq (%rax), %rsi
movq 0x28(%rsp), %rdi
callq 0x15b31
movq %rax, %r13
movq (%rax), %rdi
movq 0x8(%rax), %rsi
leaq -0x3c85(%rip), %rdx     # 0x13497
callq 0x17bcf
movq (%r13), %rax
movq 0x8(%r13), %rcx
cmpq %rax, %rcx
je 0x17333
movq %r13, %rdi
xorl %esi, %esi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x14(%rsp)
movq %r13, %rdi
xorl %esi, %esi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %r15d
xorl %r12d, %r12d
movq 0x30(%rsp), %rax
leal (%r15,%rax), %ebx
movq %r12, %r14
movq (%r13), %rax
movq 0x8(%r13), %rcx
movq %rcx, %rdx
subq %rax, %rdx
shrq $0x3, %rdx
movslq %edx, %rdx
cmpq %rdx, %r14
jge 0x17333
leaq 0x1(%r14), %r12
cmpq %rdx, %r12
jge 0x1728a
movq %r13, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
cmpl %ebx, 0xc(%rax)
jg 0x171c0
movq %r13, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movq %r12, %r14
cmpl 0x20(%rax), %r15d
jge 0x17166
jmp 0x17273
pushq $0x50
popq %rdi
callq 0x2170
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x40(%rax)
movq %rax, 0x8(%rsp)
movl 0x14(%rsp), %ecx
movl %ecx, 0xc(%rax)
movl %r15d, 0x20(%rax)
movq %r13, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movq (%rax), %rdi
callq 0x20d0
leaq 0x2(%rax), %rdi
callq 0x2040
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movq 0x8(%rsp), %rax
movq (%rax), %rbx
movq %r13, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x2140
movq %r13, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movb 0x8(%rax), %al
movq 0x8(%rsp), %rcx
movb %al, 0x8(%rcx)
movq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x7448
movq %r13, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x14(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %r15d
jmp 0x1715a
pushq $0x50
popq %rdi
callq 0x2170
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x40(%rax)
movq %rax, 0x8(%rsp)
movl 0x14(%rsp), %ecx
movl %ecx, 0xc(%rax)
movl %r15d, 0x20(%rax)
movl %r14d, %r14d
movq %r13, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movq (%rax), %rdi
callq 0x20d0
leaq 0x2(%rax), %rdi
callq 0x2040
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movq 0x8(%rsp), %rax
movq (%rax), %rbx
movq %r13, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x2140
movq %r13, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movb 0x8(%rax), %al
movq 0x8(%rsp), %rcx
movb %al, 0x8(%rcx)
movq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x7448
movq (%r13), %rax
movq 0x8(%r13), %rcx
cmpq %rax, %rcx
je 0x1733c
movq %rax, 0x8(%r13)
movq %r13, %rdi
callq 0x832c
movq %r13, %rdi
pushq $0x18
popq %rsi
callq 0x2180
incq %rbp
movq 0x20(%rsp), %rbx
jmp 0x170dd
callq 0x221f2
movq 0x18(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq