// 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)
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, %r15
callq 0x171f7
movq %rax, %r12
movl $0x18, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x10(%rsp)
movq $0x0, 0x10(%rax)
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x1b990
xorl %r13d, %r13d
movq %r15, 0x20(%rsp)
movq %r12, 0x8(%rsp)
cmpq %r13, %rdx
jbe 0x1b9c9
movq (%rax,%r13,8), %rsi
movq %r15, %rdi
callq 0x19bcb
movq %rax, %rbp
movq (%rax), %rbx
movq 0x8(%rax), %r14
cmpq %r14, %rbx
je 0x1b949
movq %r15, %r12
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 -0x47aa(%rip), %r15     # 0x16f04
movq %r15, %rcx
callq 0x1c455
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1c586
movq (%rbp), %rbx
movq 0x8(%rbp), %rax
movq %rax, %rdx
subq %rbx, %rdx
je 0x1b855
movq %r13, 0x28(%rsp)
sarq $0x3, %rdx
testl %edx, %edx
jle 0x1b931
movq (%rbx), %rcx
movl 0xc(%rcx), %r12d
movl 0x20(%rcx), %r13d
xorl %r15d, %r15d
leaq 0x1(%r15), %r14
movslq %edx, %rcx
cmpq %rcx, %r14
jge 0x1b85d
cmpq %r14, %rdx
jbe 0x1b9b8
movq 0x8(%rbx,%r15,8), %rcx
movq 0x30(%rsp), %rdx
addl %r13d, %edx
cmpl %edx, 0xc(%rcx)
jle 0x1b7e8
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, 0x18(%rsp)
movl %r12d, 0xc(%rax)
movl %r13d, 0x20(%rax)
movq (%rbp), %rax
movq 0x8(%rbp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x1b9ac
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 0x1b9ac
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 0x1b9ac
movq (%rax,%r15,8), %rax
movb 0x8(%rax), %al
movb %al, 0x8(%rbx)
movq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x1b7fa
movq %rbx, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x1b804
movl 0x20(%rcx), %edx
cmpl %edx, %r13d
cmovgl %r13d, %edx
movq %rax, %rcx
movl %edx, %r13d
jmp 0x1b833
leaq 0x18(%rsp), %rdx
callq 0xb74c
movq (%rbp), %rcx
movq 0x8(%rbp), %rax
movq %rax, %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x1b9b8
movq 0x8(%rcx,%r15,8), %rcx
movl 0xc(%rcx), %r12d
movl 0x20(%rcx), %r13d
movq (%rbp), %rbx
movq %rax, %rcx
subq %rbx, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
movq %r14, %r15
cmpq %rcx, %r14
jl 0x1b6f7
jmp 0x1b931
movq %r12, %r15
jmp 0x1b944
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, 0x18(%rsp)
movl %r12d, 0xc(%rax)
movl %r13d, 0x20(%rax)
movq (%rbp), %rax
movq 0x8(%rbp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x1b9ac
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 0x1b9ac
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 0x1b9ac
movq (%rax,%r15,8), %rax
movb 0x8(%rax), %al
movb %al, 0x8(%rbx)
movq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x1b91f
movq %rbx, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x1b929
leaq 0x18(%rsp), %rdx
callq 0xb74c
movq (%rbp), %rbx
movq 0x8(%rbp), %rax
cmpq %rbx, %rax
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %r13
je 0x1b944
movq %rbx, 0x8(%rbp)
movq 0x8(%rsp), %r12
testq %rbx, %rbx
je 0x1b95d
movq 0x10(%rbp), %rsi
subq %rbx, %rsi
movq %rbx, %rdi
callq 0x2190
movl $0x18, %esi
movq %rbp, %rdi
callq 0x2190
incq %r13
movq (%r12), %rax
movq 0x8(%r12), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r13
jl 0x1b65f
movq %r12, %rdi
callq 0x2859a
movq 0x10(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10f1d(%rip), %rdi     # 0x2c8d0
movq %r15, %rsi
jmp 0x1b9c2
leaq 0x10f11(%rip), %rdi     # 0x2c8d0
movq %r14, %rsi
xorl %eax, %eax
callq 0x2270
leaq 0x10f00(%rip), %rdi     # 0x2c8d0
movq %r13, %rsi
jmp 0x1b9c2