// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movq %rsi, %r14
movq %rdi, %rbx
pushq $0x18
popq %rdi
callq 0x2170
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x10(%rsp)
andq $0x0, 0x10(%rax)
xorl %r15d, %r15d
movq 0x8(%rbx), %rax
subq (%rbx), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r15
jge 0x1308a
movq %rbx, %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
andq $0x0, 0x48(%rax)
incq %r15
jmp 0x13060
movq %rbx, %rdi
callq 0x12b99
movq %rax, %rbx
movq %r14, %rdi
callq 0x12b99
movq %rax, %r14
pushq $0x18
popq %rdi
callq 0x2170
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movq %rax, 0x20(%rsp)
movups %xmm0, (%rax)
movq %rbx, 0x38(%rsp)
movq 0x18(%rbx), %r15
xorl %r12d, %r12d
movq %r14, 0x30(%rsp)
movq %r15, 0x28(%rsp)
movq 0x8(%r15), %rax
subq (%r15), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r12
jge 0x13410
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r12,8), %r13
movq 0x18(%r14), %rbx
movq %r15, %rdi
movq %r12, %rsi
callq 0x7342
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x21152
movq 0x8(%rbx), %rcx
subq (%rbx), %rcx
shrq $0x3, %rcx
cmpl %ecx, %eax
movq %r12, 0x40(%rsp)
jge 0x1338f
movq (%r14), %rcx
cltq
movq (%rcx,%rax,8), %r15
xorl %ebp, %ebp
movq 0x8(%r15), %rax
subq (%r15), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbp
jge 0x133f9
movq %r15, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %edi
movq 0x8(%r13), %rcx
subq (%r13), %rcx
shrq $0x3, %rcx
decl %ecx
movq %r13, %rsi
xorl %edx, %edx
callq 0x12dee
movl %eax, %r14d
movl %eax, %ebx
sarl $0x1f, %eax
andl %r14d, %eax
movl %eax, 0x1c(%rsp)
incl %r14d
testl %ebx, %ebx
jle 0x131b0
movq %r13, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %r12d
movq %r15, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
decq %rbx
decl %r14d
cmpl 0xc(%rax), %r12d
jg 0x1317e
jmp 0x131b5
movl 0x1c(%rsp), %r14d
movslq %r14d, %r14
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r14
jge 0x132e5
movq %r13, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %ebx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
cmpl 0x20(%rax), %ebx
jge 0x132e5
movq %r13, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %ebx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
cmpl 0xc(%rax), %ebx
jle 0x13223
movq %r13, %rdi
movq %r14, %rsi
jmp 0x13229
movq %r15, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %ebx
movq %r13, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %r12d
movq %r15, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
cmpl 0x20(%rax), %r12d
jge 0x13262
movq %r13, %rdi
movq %r14, %rsi
jmp 0x13268
movq %r15, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
cmpl %ebx, 0x20(%rax)
jl 0x132dd
movq %r13, %rdi
movq %r14, %rsi
callq 0x7404
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x7448
movq %r13, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %ebx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
cmpl 0x20(%rax), %ebx
jg 0x13424
movq %r13, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %ebx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
cmpl 0xc(%rax), %ebx
jl 0x13424
incq %r14
jmp 0x131b8
movq 0x20(%rsp), %rbx
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
cmpq %rdi, %rsi
je 0x13387
leaq 0x196(%rip), %rdx       # 0x13497
callq 0x17bcf
callq 0x11e71
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x7404
movq (%rax), %rax
movq (%rax), %rdi
callq 0x21062
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x7404
movq (%rax), %rax
movb 0x8(%rax), %al
movq 0x8(%rsp), %rcx
movb %al, 0x8(%rcx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %eax
movq 0x8(%rsp), %rcx
movl %eax, 0xc(%rcx)
movq 0x8(%rbx), %rax
movq -0x8(%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x20(%rcx)
movq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x7448
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x13387
movq %rax, 0x8(%rbx)
incq %rbp
jmp 0x1312d
movq 0x21c4a(%rip), %rax     # 0x34fe0
movq (%rax), %rbx
movq %r15, %rdi
movq %r12, %rsi
callq 0x7342
movq (%rax), %rdx
movq %rbx, %rdi
leaq 0x145be(%rip), %rsi     # 0x2796f
xorl %eax, %eax
callq 0x2240
xorl %ebx, %ebx
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbx
jge 0x133f9
movq %r13, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rdi
callq 0x134b2
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x7448
incq %rbx
jmp 0x133ba
movq 0x40(%rsp), %r12
incq %r12
movq 0x30(%rsp), %r14
movq 0x28(%rsp), %r15
jmp 0x130ce
movq 0x10(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x21bb5(%rip), %rax     # 0x34fe0
movq (%rax), %rbx
movq %r13, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %r12d
movq %r13, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %r14d
movq %r15, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %r13d
movq %r15, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %r9d
leaq 0x144b9(%rip), %rsi     # 0x27936
movq %rbx, %rdi
movl %r12d, %edx
movl %r14d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x2240
xorl %edi, %edi
callq 0x21d0