// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movq %rsi, %r14
movq %rdi, %r15
movl $0x18, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq (%r15), %r8
movq 0x8(%r15), %rdx
subq %r8, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x16a3f
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x16ef8
movq (%r8,%rsi,8), %rdi
movq $0x0, 0x48(%rdi)
incq %rsi
cmpq %rsi, %rcx
jne 0x16a22
movq %rax, 0x10(%rsp)
movq %r15, %rdi
callq 0x163f1
movq %rax, %rbx
movq %r14, %rdi
callq 0x163f1
movq %rax, %r13
movl $0x18, %edi
callq 0x2180
movq %rax, %r12
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %rbx, 0x18(%rsp)
movq 0x18(%rbx), %rbp
movq (%rbp), %rax
movq 0x8(%rbp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x16e82
leaq 0x8(%rbp), %r14
xorl %esi, %esi
movq %r13, 0x30(%rsp)
cmpq %rsi, %rdx
jbe 0x16ea2
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movq (%rcx,%rsi,8), %r15
movq 0x18(%r13), %rbx
movq %rsi, 0x20(%rsp)
movq (%rax,%rsi,8), %rsi
movq %rbx, %rdi
callq 0x26ed0
movq 0x8(%rbx), %rcx
subq (%rbx), %rcx
shrq $0x3, %rcx
cmpl %ecx, %eax
jge 0x16dcc
movq (%r13), %rcx
cltq
movq (%rcx,%rax,8), %r13
movq (%r13), %rax
movq 0x8(%r13), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x16d94
xorl %r14d, %r14d
movq %r15, 0x28(%rsp)
cmpq %r14, %rdx
jbe 0x16eba
movq (%rax,%r14,8), %rax
movl 0xc(%rax), %edi
movq 0x8(%r15), %rcx
subq (%r15), %rcx
shrq $0x3, %rcx
decl %ecx
movq %r15, %rsi
xorl %edx, %edx
callq 0x167a8
movq (%r15), %rdi
movq 0x8(%r15), %r8
movq %r8, %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
testl %eax, %eax
jle 0x16b81
movl %eax, %esi
cmpq %rsi, %rdx
jbe 0x16ea2
movq (%r13), %r9
movq 0x8(%r13), %rcx
subq %r9, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
jbe 0x16eab
movq (%rdi,%rsi,8), %rcx
movl 0x20(%rcx), %ecx
movq (%r9,%r14,8), %rsi
cmpl 0xc(%rsi), %ecx
jle 0x16b81
leal -0x1(%rax), %ecx
cmpl $0x1, %eax
movl %ecx, %eax
jg 0x16b42
xorl %eax, %eax
cmpl %edx, %eax
jge 0x16c6c
movslq %eax, %rbp
cmpq %rbp, %rdx
jbe 0x16e96
movq (%r13), %rcx
movq 0x8(%r13), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x16eba
movq (%rdi,%rbp,8), %rax
movl 0xc(%rax), %esi
movq (%rcx,%r14,8), %rcx
movl 0x20(%rcx), %edx
cmpl %edx, %esi
jge 0x16c6c
cmpl 0xc(%rcx), %esi
movq %rcx, %rsi
cmovgq %rax, %rsi
cmpl %edx, 0x20(%rax)
cmovlq %rax, %rcx
movl 0x20(%rcx), %ecx
cmpl 0xc(%rsi), %ecx
jge 0x16be8
movq %r8, %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
jmp 0x16c5d
movq 0x8(%r12), %rsi
cmpq 0x10(%r12), %rsi
je 0x16bff
movq %rax, (%rsi)
addq $0x8, 0x8(%r12)
jmp 0x16c0b
leaq (%rdi,%rbp,8), %rdx
movq %r12, %rdi
callq 0xb74c
movq (%r15), %rdi
movq 0x8(%r15), %r8
movq %r8, %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
cmpq %rbp, %rdx
jbe 0x16e96
movq (%r13), %rcx
movq 0x8(%r13), %rax
subq %rcx, %rax
sarq $0x3, %rax
cmpq %r14, %rax
jbe 0x16ed7
movq (%rdi,%rbp,8), %rax
movl 0x20(%rax), %esi
movq (%rcx,%r14,8), %rcx
cmpl 0x20(%rcx), %esi
jg 0x16ec6
movl 0xc(%rax), %eax
cmpl 0xc(%rcx), %eax
jl 0x16ec6
incq %rbp
movslq %edx, %rax
cmpq %rax, %rbp
jl 0x16b8c
movq (%r12), %rbx
movq 0x8(%r12), %rbp
movq %rbp, %rax
subq %rbx, %rax
je 0x16d66
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x265(%rip), %r15       # 0x16f04
movq %r15, %rcx
callq 0x1c455
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x1c586
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, 0x8(%rsp)
movq (%r12), %rax
cmpq %rax, 0x8(%r12)
je 0x16ee6
movq (%rax), %rax
movq (%rax), %rdi
callq 0x26db4
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movq (%r12), %rax
movq 0x8(%r12), %rcx
cmpq %rax, %rcx
je 0x16ee6
movq (%rax), %rdx
movb 0x8(%rdx), %sil
movq 0x8(%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 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x16d47
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x16d51
leaq 0x8(%rsp), %rdx
callq 0xb74c
movq 0x28(%rsp), %r15
movq (%r12), %rax
cmpq %rax, 0x8(%r12)
je 0x16d66
movq %rax, 0x8(%r12)
incq %r14
movq (%r13), %rax
movq 0x8(%r13), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r14
jl 0x16b06
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rbp
movq 0x20(%rsp), %rsi
incq %rsi
leaq 0x8(%rbp), %r14
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rsi
movq 0x30(%rsp), %r13
jl 0x16aa0
jmp 0x16e82
movq (%r14), %rdx
movq (%rbp), %rax
subq %rax, %rdx
sarq $0x3, %rdx
movq 0x20(%rsp), %rsi
cmpq %rsi, %rdx
jbe 0x16ea2
movq 0x221f1(%rip), %rcx     # 0x38fe0
movq (%rcx), %rdi
movq (%rax,%rsi,8), %rdx
leaq 0x16b72(%rip), %rsi     # 0x2d96f
xorl %eax, %eax
callq 0x2250
movq (%r15), %rax
movq 0x8(%r15), %rdx
movq %rdx, %rcx
subq %rax, %rcx
shrq $0x3, %rcx
testl %ecx, %ecx
jle 0x16d94
xorl %r14d, %r14d
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x16eba
movq (%rax,%r14,8), %rdi
callq 0x16f1f
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x16e57
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x16e61
leaq 0x8(%rsp), %rdx
callq 0xb74c
incq %r14
movq (%r15), %rax
movq 0x8(%r15), %rdx
movq %rdx, %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r14
jl 0x16e20
jmp 0x16d94
movq 0x10(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x15a33(%rip), %rdi     # 0x2c8d0
movq %rbp, %rsi
jmp 0x16ef1
leaq 0x15a27(%rip), %rdi     # 0x2c8d0
jmp 0x16ef1
leaq 0x15a1e(%rip), %rdi     # 0x2c8d0
movq %r14, %rsi
movq %rcx, %rdx
jmp 0x16ef1
leaq 0x15a0f(%rip), %rdi     # 0x2c8d0
movq %r14, %rsi
jmp 0x16ef1
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x4ad2
leaq 0x159f2(%rip), %rdi     # 0x2c8d0
movq %r14, %rsi
movq %rax, %rdx
jmp 0x16ef1
leaq 0x159e3(%rip), %rdi     # 0x2c8d0
xorl %esi, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2270
leaq 0x159d1(%rip), %rdi     # 0x2c8d0
movq %rdx, %rsi
jmp 0x16ef1