// File: 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:
subq $0xb8, %rsp
movq %rdi, 0xb0(%rsp)
movl %esi, 0xac(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x2a910
movq %rax, 0xa0(%rsp)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x50(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x50(%rsp), %rdi
callq 0xc670
movq 0x50(%rsp), %rax
movq %rax, 0x98(%rsp)
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0x4c(%rsp)
movq 0xa0(%rsp), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x4c(%rsp), %eax
cmpl %ecx, %eax
jge 0x30d9d
movq 0xb0(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0xa0(%rsp), %rdi
movslq 0x94(%rsp), %rsi
callq 0xc240
movq 0x40(%rsp), %rdi
movq (%rax), %rsi
callq 0x2e1e0
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rdi
callq 0xc7d0
movq %rax, 0x80(%rsp)
movq 0x88(%rsp), %rdi
callq 0xc760
movq %rax, 0x78(%rsp)
movq 0x80(%rsp), %rdi
movq 0x78(%rsp), %rsi
leaq -0x6654(%rip), %rdx     # 0x2a3a0
callq 0x31d00
movq 0x88(%rsp), %rdi
callq 0xc680
cmpq $0x0, %rax
jbe 0x30d4c
movl $0x0, 0x74(%rsp)
movq 0x88(%rsp), %rdi
movslq 0x74(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x70(%rsp)
movq 0x88(%rsp), %rdi
movslq 0x74(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x6c(%rsp)
movl 0x74(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq 0x88(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0x3c(%rsp), %eax
cmpl %ecx, %eax
jge 0x30d4a
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
movq 0x88(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0x38(%rsp), %eax
cmpl %ecx, %eax
jge 0x30c56
movq 0x88(%rsp), %rdi
movl 0x74(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl 0x6c(%rsp), %ecx
addl 0xac(%rsp), %ecx
cmpl %ecx, %eax
jg 0x30b2d
movl 0x6c(%rsp), %eax
movl %eax, 0x34(%rsp)
movq 0x88(%rsp), %rdi
movl 0x74(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0xc800
movq %rax, %rcx
movl 0x34(%rsp), %eax
movq (%rcx), %rcx
cmpl 0x20(%rcx), %eax
jge 0x30b18
movq 0x88(%rsp), %rdi
movl 0x74(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x30(%rsp)
jmp 0x30b20
movl 0x6c(%rsp), %eax
movl %eax, 0x30(%rsp)
movl 0x30(%rsp), %eax
movl %eax, 0x6c(%rsp)
jmp 0x30c51
movl $0x50, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x20(%rsp)
xorl %esi, %esi
movl $0x50, %edx
callq 0x2110
movq 0x20(%rsp), %rax
movq %rax, 0x60(%rsp)
movl 0x70(%rsp), %ecx
movq 0x60(%rsp), %rax
movl %ecx, 0xc(%rax)
movl 0x6c(%rsp), %ecx
movq 0x60(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x60(%rsp), %rax
movq $0x0, 0x18(%rax)
movq 0x88(%rsp), %rdi
movslq 0x74(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movq (%rax), %rdi
callq 0x20e0
movq %rax, %rdi
addq $0x2, %rdi
callq 0x2040
movq %rax, %rcx
movq 0x60(%rsp), %rax
movq %rcx, (%rax)
movq 0x60(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x88(%rsp), %rdi
movslq 0x74(%rsp), %rsi
callq 0xc800
movq 0x28(%rsp), %rdi
movq (%rax), %rax
movq (%rax), %rsi
callq 0x2160
movq 0x88(%rsp), %rdi
movslq 0x74(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movb 0x8(%rax), %cl
movq 0x60(%rsp), %rax
movb %cl, 0x8(%rax)
movq 0x98(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xc8c0
movq 0x88(%rsp), %rdi
movl 0x74(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x70(%rsp)
movq 0x88(%rsp), %rdi
movl 0x74(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x6c(%rsp)
jmp 0x30d3a
movl $0x50, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x10(%rsp)
xorl %esi, %esi
movl $0x50, %edx
callq 0x2110
movq 0x10(%rsp), %rax
movq %rax, 0x58(%rsp)
movl 0x70(%rsp), %ecx
movq 0x58(%rsp), %rax
movl %ecx, 0xc(%rax)
movl 0x6c(%rsp), %ecx
movq 0x58(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x58(%rsp), %rax
movq $0x0, 0x18(%rax)
movq 0x88(%rsp), %rdi
movslq 0x74(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movq (%rax), %rdi
callq 0x20e0
movq %rax, %rdi
addq $0x2, %rdi
callq 0x2040
movq %rax, %rcx
movq 0x58(%rsp), %rax
movq %rcx, (%rax)
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x88(%rsp), %rdi
movslq 0x74(%rsp), %rsi
callq 0xc800
movq 0x18(%rsp), %rdi
movq (%rax), %rax
movq (%rax), %rsi
callq 0x2160
movq 0x88(%rsp), %rdi
movslq 0x74(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movb 0x8(%rax), %cl
movq 0x58(%rsp), %rax
movb %cl, 0x8(%rax)
movq 0x98(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0xc8c0
jmp 0x30d4a
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x30a50
jmp 0x30d4c
movq 0x88(%rsp), %rdi
callq 0x31d90
movq 0x88(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x30d85
movq 0x8(%rsp), %rdi
callq 0xc970
movq 0x8(%rsp), %rdi
movl $0x18, %esi
callq 0x21a0
jmp 0x30d87
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x3095b
movq 0xa0(%rsp), %rdi
callq 0x49db0
movq 0x98(%rsp), %rax
addq $0xb8, %rsp
retq
nopw (%rax,%rax)