// File: /workspace/github_cpp_makefile_star2_O1/gersteinlab[P]MUSIC/src/ms_annot_region_tools.cpp
// Function: merge_regions_per_super_regions(std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, std::vector<t_annot_region*, std::allocator<t_annot_region*>>*)
vector<t_annot_region*>* merge_regions_per_super_regions(vector<t_annot_region*>* regions, vector<t_annot_region*>* super_regions)
{
	vector<t_annot_region*>* merged_regions = new vector<t_annot_region*>();

	for(int i_reg = 0; i_reg < (int)regions->size(); i_reg++)
	{
		regions->at(i_reg)->data = NULL;
	} // i_reg loop

	// Go over all the super regions, overlap them with regions, then merge the regions that overlap with the same super region.
	t_sorted_annot_region_lists* restructured_regions = restructure_annot_regions(regions);
	t_sorted_annot_region_lists* restructured_super_regions = restructure_annot_regions(super_regions);

	// Go over all the super regions.
	vector<t_annot_region*>* cur_regions = new vector<t_annot_region*>();

	for(int i_chr = 0; i_chr < (int)restructured_regions->chr_ids->size(); i_chr++)
	{
		vector<t_annot_region*>* cur_chrom_regions = restructured_regions->regions_per_chrom[i_chr];

		int i_sup_chr = t_string::get_i_str(restructured_super_regions->chr_ids, restructured_regions->chr_ids->at(i_chr));

		if(i_sup_chr < (int)restructured_super_regions->chr_ids->size())
		{			
			vector<t_annot_region*>* cur_chrom_super_regions = restructured_super_regions->regions_per_chrom[i_sup_chr];

			// Go over all the super regions, merge the regions that overlap with the same super region.
			for(int i_sup_reg = 0; i_sup_reg < (int)cur_chrom_super_regions->size(); i_sup_reg++)
			{
				// Find all the regions that are overlapping with the current super region.
				int i_left_reg = locate_posn_region_per_region_starts(cur_chrom_super_regions->at(i_sup_reg)->start, 
																		cur_chrom_regions, 0, (int)cur_chrom_regions->size()-1);

				while(i_left_reg > 0 &&
					cur_chrom_regions->at(i_left_reg)->end > cur_chrom_super_regions->at(i_sup_reg)->start)
				{
					i_left_reg--;
				}

				while(i_left_reg < (int)cur_chrom_regions->size() &&
					cur_chrom_regions->at(i_left_reg)->start < cur_chrom_super_regions->at(i_sup_reg)->end)
				{
					int ol_start = MAX(cur_chrom_regions->at(i_left_reg)->start, cur_chrom_super_regions->at(i_sup_reg)->start);
					int ol_end = MIN(cur_chrom_regions->at(i_left_reg)->end, cur_chrom_super_regions->at(i_sup_reg)->end);

					// Check the overlap.
					if(ol_end >= ol_start)
					{
						cur_regions->push_back(cur_chrom_regions->at(i_left_reg));

						// Sanity check: Make sure whole region is contained in the super region.
						if(cur_chrom_regions->at(i_left_reg)->end > cur_chrom_super_regions->at(i_sup_reg)->end ||
							cur_chrom_regions->at(i_left_reg)->start < cur_chrom_super_regions->at(i_sup_reg)->start)
						{
							fprintf(stderr, "Sanity check failed: Region: %d-%d, Super-region: %d-%d\n", 
								cur_chrom_regions->at(i_left_reg)->start, cur_chrom_regions->at(i_left_reg)->end, 
								cur_chrom_super_regions->at(i_sup_reg)->start, cur_chrom_super_regions->at(i_sup_reg)->end);

							exit(0);
						}
					}

					i_left_reg++;
				} // i_left_reg loop.

				if(cur_regions->size() > 0)
				{
					sort(cur_regions->begin(), cur_regions->end(), sort_regions);

					// Merge all the regions in the overlapping regions.
					t_annot_region* cur_merged_region = get_empty_region();
					cur_merged_region->chrom = t_string::copy_me_str(cur_regions->at(0)->chrom);
					cur_merged_region->strand = cur_regions->at(0)->strand;
					cur_merged_region->start = cur_regions->at(0)->start;
					cur_merged_region->end = cur_regions->back()->end;
					merged_regions->push_back(cur_merged_region);

					// Clear and delete current regions.
					cur_regions->clear();
				}				
			} // i_sup_reg loop.
		} // i_sup_chr check.
		else
		{
			fprintf(stderr, "Could not find %s in super regions list.\n", restructured_regions->chr_ids->at(i_chr));

			// Add all the regions in this chromosome.
			for(int i_reg = 0; i_reg < (int)cur_chrom_regions->size(); i_reg++)
			{
				t_annot_region* cur_merged_region = duplicate_region(cur_chrom_regions->at(i_reg));
				merged_regions->push_back(cur_merged_region);
			} // i_reg loop.
		}
	} // i_chr loop.

	return(merged_regions);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movq %rdi, %r15
movl $0x18, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x8(%rsp)
movq $0x0, 0x10(%rax)
movq (%r15), %rax
movq 0x8(%r15), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x16de9
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x17256
movq (%rax,%rsi,8), %rdi
movq $0x0, 0x48(%rdi)
incq %rsi
cmpq %rsi, %rcx
jne 0x16dcc
movq %r15, %rdi
callq 0x16742
movq %rax, %rbx
movq %r14, %rdi
callq 0x16742
movq %rax, %rbp
movl $0x18, %edi
callq 0x2180
movq %rax, %r12
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %rbx, 0x10(%rsp)
movq 0x18(%rbx), %r15
movq (%r15), %rax
movq 0x8(%r15), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x1722a
leaq 0x8(%r15), %r13
xorl %esi, %esi
movq %rbp, 0x20(%rsp)
cmpq %rsi, %rdx
jbe 0x17256
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq (%rcx,%rsi,8), %r14
movq 0x18(%rbp), %rbx
movq %rsi, 0x18(%rsp)
movq (%rax,%rsi,8), %rsi
movq %rbx, %rdi
callq 0x274fc
movq 0x8(%rbx), %rcx
subq (%rbx), %rcx
shrq $0x3, %rcx
cmpl %ecx, %eax
jge 0x1717f
movq (%rbp), %rcx
cltq
movq (%rcx,%rax,8), %r13
movq (%r13), %rax
movq 0x8(%r13), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x17144
xorl %r15d, %r15d
cmpq %r15, %rdx
jbe 0x1723e
movq (%rax,%r15,8), %rax
movl 0xc(%rax), %edi
movq 0x8(%r14), %rcx
subq (%r14), %rcx
shrq $0x3, %rcx
decl %ecx
movq %r14, %rsi
xorl %edx, %edx
callq 0x16b4d
testl %eax, %eax
jle 0x16f1d
movq (%r14), %rdi
movq 0x8(%r14), %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
movl %eax, %esi
cmpq %rsi, %rdx
jbe 0x17256
movq (%r13), %r8
movq 0x8(%r13), %rcx
subq %r8, %rcx
sarq $0x3, %rcx
cmpq %r15, %rcx
jbe 0x1725f
movq (%rdi,%rsi,8), %rcx
movl 0x20(%rcx), %ecx
movq (%r8,%r15,8), %rsi
cmpl 0xc(%rsi), %ecx
jle 0x16f1d
leal -0x1(%rax), %ecx
cmpl $0x1, %eax
movl %ecx, %eax
jg 0x16ede
xorl %eax, %eax
movq (%r14), %rcx
movq 0x8(%r14), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpl %edx, %eax
jge 0x17029
movslq %eax, %rbp
cmpq %rbp, %rdx
jbe 0x1724a
movq (%r13), %rsi
movq 0x8(%r13), %rdx
subq %rsi, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x1723e
movq (%rcx,%rbp,8), %rax
movl 0xc(%rax), %r8d
movq (%rsi,%r15,8), %r9
movl 0x20(%r9), %edi
cmpl %edi, %r8d
jge 0x17029
leaq (%rcx,%rbp,8), %rdx
leaq (%rsi,%r15,8), %rcx
cmpl 0xc(%r9), %r8d
movq %rcx, %rsi
cmovgq %rdx, %rsi
movq (%rsi), %rsi
cmpl %edi, 0x20(%rax)
cmovlq %rdx, %rcx
movq (%rcx), %rcx
movl 0x20(%rcx), %ecx
cmpl 0xc(%rsi), %ecx
jl 0x17006
movq 0x8(%r12), %rsi
cmpq 0x10(%r12), %rsi
je 0x16faf
movq %rax, (%rsi)
addq $0x8, 0x8(%r12)
jmp 0x16fb7
movq %r12, %rdi
callq 0xb976
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbp, %rdx
jbe 0x1724a
movq (%r13), %rcx
movq 0x8(%r13), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x1723e
movq (%rax,%rbp,8), %rax
movl 0x20(%rax), %edx
movq (%rcx,%r15,8), %rcx
cmpl 0x20(%rcx), %edx
jg 0x17273
movl 0xc(%rax), %eax
cmpl 0xc(%rcx), %eax
jl 0x17273
incq %rbp
movq (%r14), %rcx
movq 0x8(%r14), %rax
subq %rcx, %rax
movq %rax, %rdx
sarq $0x3, %rdx
shrq $0x3, %rax
cltq
cmpq %rax, %rbp
jl 0x16f36
movq (%r12), %rbx
movq 0x8(%r12), %rbp
movq %rbp, %rax
subq %rbx, %rax
je 0x1711a
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x235(%rip), %rcx       # 0x17291
callq 0x1c869
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x223(%rip), %rdx       # 0x17291
callq 0x1c99a
movl $0x50, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x40(%rax)
movq %rax, (%rsp)
movq (%r12), %rax
cmpq %rax, 0x8(%r12)
je 0x17284
movq (%rax), %rax
movq (%rax), %rdi
callq 0x273e0
movq (%rsp), %rcx
movq %rax, (%rcx)
movq (%r12), %rax
movq 0x8(%r12), %rcx
cmpq %rax, %rcx
je 0x17284
movq (%rax), %rdx
movb 0x8(%rdx), %sil
movq (%rsp), %rax
movb %sil, 0x8(%rax)
movl 0xc(%rdx), %edx
movl %edx, 0xc(%rax)
movq -0x8(%rcx), %rcx
movl 0x20(%rcx), %ecx
movl %ecx, 0x20(%rax)
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x17102
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x1710a
movq %rsp, %rdx
callq 0xb976
movq (%r12), %rax
cmpq %rax, 0x8(%r12)
je 0x1711a
movq %rax, 0x8(%r12)
incq %r15
movq (%r13), %rax
movq 0x8(%r13), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r15
movq 0x20(%rsp), %rbp
jl 0x16ea5
movq 0x18(%rsp), %rsi
incq %rsi
movq 0x10(%rsp), %rax
movq 0x18(%rax), %r15
leaq 0x8(%r15), %r13
movq (%r15), %rax
movq 0x8(%r15), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rsi
jl 0x16e44
jmp 0x1722a
movq (%r13), %rdx
movq (%r15), %rax
subq %rax, %rdx
sarq $0x3, %rdx
movq 0x18(%rsp), %rsi
cmpq %rsi, %rdx
jbe 0x17256
movq 0x22e3e(%rip), %rcx     # 0x39fe0
movq (%rcx), %rdi
movq (%rax,%rsi,8), %rdx
leaq 0x177bf(%rip), %rsi     # 0x2e96f
xorl %eax, %eax
callq 0x2250
movq 0x8(%r14), %rdx
movq %rdx, %rax
subq (%r14), %rax
shrq $0x3, %rax
testl %eax, %eax
jle 0x17144
xorl %r15d, %r15d
movq (%r14), %rax
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x1723e
movq (%rax,%r15,8), %rdi
callq 0x172ac
movq %rax, (%rsp)
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x17205
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x1720d
movq %rsp, %rdx
callq 0xb976
movq 0x8(%r14), %rdx
movq %rdx, %rax
subq (%r14), %rax
incq %r15
shrq $0x3, %rax
cltq
cmpq %rax, %r15
jl 0x171d0
jmp 0x17144
movq 0x8(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x16691(%rip), %rdi     # 0x2d8d6
movq %r15, %rsi
jmp 0x1726c
leaq 0x16685(%rip), %rdi     # 0x2d8d6
movq %rbp, %rsi
jmp 0x1726c
leaq 0x16679(%rip), %rdi     # 0x2d8d6
jmp 0x1726c
leaq 0x16670(%rip), %rdi     # 0x2d8d6
movq %r15, %rsi
movq %rcx, %rdx
xorl %eax, %eax
callq 0x2270
movq %r14, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x4c3d
leaq 0x1664b(%rip), %rdi     # 0x2d8d6
xorl %esi, %esi
xorl %edx, %edx
jmp 0x1726c