// 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)
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, 0x20(%rsp)
callq 0x1756e
movq %rax, %rbx
movl $0x18, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x8(%rsp)
movq $0x0, 0x10(%rax)
movq (%rbx), %rax
movq %rbx, 0x18(%rsp)
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x1bda6
xorl %esi, %esi
cmpq %rsi, %rdx
jbe 0x1bde1
movq %rsi, 0x28(%rsp)
movq (%rax,%rsi,8), %rsi
movq 0x20(%rsp), %rdi
callq 0x1a008
movq %rax, %rbp
movq (%rax), %rbx
movq 0x8(%rax), %r14
cmpq %r14, %rbx
je 0x1bae4
movq %r14, %rax
subq %rbx, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbx, %rdi
movq %r14, %rsi
leaq -0x483d(%rip), %r15     # 0x17291
movq %r15, %rcx
callq 0x1c869
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1c99a
movq (%rbp), %rcx
cmpq %rcx, 0x8(%rbp)
je 0x1bd4c
movq (%rbp), %rax
movq 0x8(%rbp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x1bd4c
movq (%rcx), %rcx
movl 0xc(%rcx), %r13d
movl 0x20(%rcx), %r12d
xorl %r15d, %r15d
leaq 0x1(%r15), %r14
movslq %edx, %rcx
cmpq %rcx, %r14
jge 0x1bc78
cmpq %r14, %rdx
jbe 0x1bdd0
movq 0x8(%rax,%r15,8), %rax
movq 0x30(%rsp), %rcx
addl %r12d, %ecx
cmpl %ecx, 0xc(%rax)
jle 0x1bc10
movl $0x50, %edi
callq 0x2180
movq %rax, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x40(%rax)
movq %rax, 0x10(%rsp)
movl %r13d, 0xc(%rax)
movl %r12d, 0x20(%rax)
movq $0x0, 0x18(%rax)
movq (%rbp), %rax
movq 0x8(%rbp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x1bdc4
movq (%rax,%r15,8), %rax
movq (%rax), %rdi
callq 0x20d0
leaq 0x2(%rax), %rdi
callq 0x2040
movq %rax, (%rbx)
movq (%rbp), %rcx
movq 0x8(%rbp), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x1bdc4
movq (%rcx,%r15,8), %rcx
movq (%rcx), %rsi
movq %rax, %rdi
callq 0x2150
movq (%rbp), %rax
movq 0x8(%rbp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x1bdc4
movq (%rax,%r15,8), %rax
movb 0x8(%rax), %al
movb %al, 0x8(%rbx)
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x1bc1f
movq %rbx, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x1bc29
movl 0x20(%rax), %eax
cmpl %eax, %r12d
cmovgl %r12d, %eax
movl %eax, %r12d
jmp 0x1bc4e
leaq 0x10(%rsp), %rdx
callq 0xb976
movq (%rbp), %rax
movq 0x8(%rbp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x1bdd0
movq 0x8(%rax,%r15,8), %rax
movl 0xc(%rax), %r13d
movl 0x20(%rax), %r12d
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
movq %r14, %r15
cmpq %rcx, %r14
jl 0x1bb17
jmp 0x1bd4c
movl $0x50, %edi
callq 0x2180
movq %rax, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x40(%rax)
movq %rax, 0x10(%rsp)
movl %r13d, 0xc(%rax)
movl %r12d, 0x20(%rax)
movq $0x0, 0x18(%rax)
movq (%rbp), %rax
movq 0x8(%rbp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x1bdc4
movq (%rax,%r15,8), %rax
movq (%rax), %rdi
callq 0x20d0
leaq 0x2(%rax), %rdi
callq 0x2040
movq %rax, (%rbx)
movq (%rbp), %rcx
movq 0x8(%rbp), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x1bdc4
movq (%rcx,%r15,8), %rcx
movq (%rcx), %rsi
movq %rax, %rdi
callq 0x2150
movq (%rbp), %rax
movq 0x8(%rbp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x1bdc4
movq (%rax,%r15,8), %rax
movb 0x8(%rax), %al
movb %al, 0x8(%rbx)
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x1bd42
movq %rbx, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x1bd4c
leaq 0x10(%rsp), %rdx
callq 0xb976
movq (%rbp), %rdi
cmpq %rdi, 0x8(%rbp)
je 0x1bd5a
movq %rdi, 0x8(%rbp)
testq %rdi, %rdi
je 0x1bd6b
movq 0x10(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
movl $0x18, %esi
movq %rbp, %rdi
callq 0x2190
movq 0x28(%rsp), %rsi
incq %rsi
movq 0x18(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rsi
jl 0x1ba7f
movq 0x18(%rsp), %rdi
callq 0x28cb4
movq 0x8(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11b0b(%rip), %rdi     # 0x2d8d6
movq %r15, %rsi
jmp 0x1bdda
leaq 0x11aff(%rip), %rdi     # 0x2d8d6
movq %r14, %rsi
xorl %eax, %eax
callq 0x2270
leaq 0x11aee(%rip), %rdi     # 0x2d8d6
jmp 0x1bdda