// File: /workspace/github_cpp_makefile_star2_O1/gersteinlab[P]MUSIC/src/ms_annot_region_tools.cpp
// Function: get_inter_region_distance_distribution(std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, std::vector<t_annot_region*, std::allocator<t_annot_region*>>*)
vector<int>* get_inter_region_distance_distribution(vector<t_annot_region*>* annot_regions1, 
	vector<t_annot_region*>* annot_regions2)
{
	int max_dist = 10*1000*1000;

	vector<int>* distances = new vector<int>();

	t_sorted_annot_region_lists* restr_regs1 = restructure_annot_regions(annot_regions1);
	t_sorted_annot_region_lists* restr_regs2 = restructure_annot_regions(annot_regions2);

	for(int i_chr1 = 0; i_chr1 < (int)restr_regs1->chr_ids->size(); i_chr1++)
	{
		fprintf(stderr, "Processing regions in %s\n", restr_regs1->chr_ids->at(i_chr1));

		int i_chr2 = t_string::get_i_str(restr_regs2->chr_ids, restr_regs1->chr_ids->at(i_chr1));
		vector<t_annot_region*>* cur_chr_regs1 = restr_regs1->regions_per_chrom[i_chr1];

		if(i_chr2 < (int)restr_regs2->chr_ids->size())
		{			
			vector<t_annot_region*>* cur_chr_regs2 = restr_regs2->regions_per_chrom[i_chr2];			

			for(int i_reg1 = 0; i_reg1 < (int)cur_chr_regs1->size(); i_reg1++)
			{
				fprintf(stderr, "Searching for %s:%d-%d\n", cur_chr_regs1->at(i_reg1)->chrom, cur_chr_regs1->at(i_reg1)->start, cur_chr_regs1->at(i_reg1)->end);

				int cur_reg_min_distance = max_dist;

				int i_loc_reg2 = locate_posn_region_per_region_starts(cur_chr_regs1->at(i_reg1)->start, cur_chr_regs2, 0, (int)cur_chr_regs2->size()-1);

				while(i_loc_reg2 > 0 &&
					cur_chr_regs2->at(i_loc_reg2)->end > cur_chr_regs1->at(i_reg1)->start)
				{
					i_loc_reg2--;
				}

				while(i_loc_reg2 < (int)cur_chr_regs2->size() &&
					cur_chr_regs2->at(i_loc_reg2)->start < cur_chr_regs1->at(i_reg1)->end)
				{
					// Get the distance of the current region2 to region1.
					int cur_dist = get_reg2reg_distance(cur_chr_regs1->at(i_reg1), cur_chr_regs2->at(i_loc_reg2));

					if(cur_reg_min_distance > cur_dist)
					{
						cur_reg_min_distance = cur_dist;
						//fprintf(stderr, "5' close exon: %s:%d-%d\n", cur_chr_regs2->at(i_loc_reg2)->chrom, cur_chr_regs2->at(i_loc_reg2)->start, cur_chr_regs2->at(i_loc_reg2)->end);
						//getc(stdin);
					}

					i_loc_reg2++;
				} // i_loc_reg2 loop.

				// The above loop is out when the reg2 passes over reg1. Must check it again. On the right.
				if(i_loc_reg2 < (int)cur_chr_regs2->size())
				{
					int cur_dist = get_reg2reg_distance(cur_chr_regs1->at(i_reg1), cur_chr_regs2->at(i_loc_reg2));

					if(cur_reg_min_distance > cur_dist)
					{
						cur_reg_min_distance = cur_dist;
						//fprintf(stderr, "3' close exon: %s:%d-%d\n", cur_chr_regs2->at(i_loc_reg2)->chrom, cur_chr_regs2->at(i_loc_reg2)->start, cur_chr_regs2->at(i_loc_reg2)->end);
						//getc(stdin);
					}
				}

				fprintf(stderr, "%s:%d-%d: %d\n", cur_chr_regs1->at(i_reg1)->chrom, cur_chr_regs1->at(i_reg1)->start, cur_chr_regs1->at(i_reg1)->end, cur_reg_min_distance);

				distances->push_back(cur_reg_min_distance);
			} // i_reg1 loop.
		} // i_chr2 check.
		else
		{

			fprintf(stderr, "Could not find %s in chr2 list.\n", restr_regs1->chr_ids->at(i_chr1));
			getc(stdin);

			// Add 10000000 to the distances.
			for(int i_reg1 = 0; i_reg1 < (int)cur_chr_regs1->size(); i_reg1++)
			{
				distances->push_back(max_dist);
			} // i_reg1 loop.
		}
	} // i_chr1 loop.

	return(distances);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r15
movq %rdi, %r14
movl $0x989680, 0xc(%rsp)    # imm = 0x989680
movl $0x18, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x10(%rsp)
movq $0x0, 0x10(%rax)
movq %r14, %rdi
callq 0x16742
movq %rax, %rbx
movq %r15, %rdi
callq 0x16742
movq %rax, 0x18(%rsp)
movq %rbx, 0x28(%rsp)
movq 0x18(%rbx), %rbx
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x19dfc
movabsq $0x100000000, %r15      # imm = 0x100000000
xorl %r12d, %r12d
movq %rbx, 0x20(%rsp)
cmpq %r12, %rdx
jbe 0x19e45
movq 0x20566(%rip), %rcx     # 0x39fe0
movq (%rcx), %rdi
movq (%rax,%r12,8), %rdx
leaq 0x15295(%rip), %rsi     # 0x2ed1d
xorl %eax, %eax
callq 0x2250
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r12, %rdx
jbe 0x19e45
movq 0x18(%rsp), %rcx
movq 0x18(%rcx), %r13
movq (%rax,%r12,8), %rsi
movq %r13, %rdi
callq 0x274fc
movq 0x28(%rsp), %rcx
movq (%rcx), %rcx
movq (%rcx,%r12,8), %r14
movq 0x8(%r13), %rcx
subq (%r13), %rcx
shrq $0x3, %rcx
cmpl %ecx, %eax
movq %r12, 0x30(%rsp)
jge 0x19d66
movq (%r14), %rcx
movq 0x8(%r14), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x19d33
movq 0x18(%rsp), %rsi
movq (%rsi), %rsi
cltq
movq (%rsi,%rax,8), %r13
xorl %ebp, %ebp
cmpq %rbp, %rdx
jbe 0x19e34
movq 0x204ca(%rip), %rax     # 0x39fe0
movq (%rax), %rdi
movq (%rcx,%rbp,8), %rax
movq (%rax), %rdx
movl 0xc(%rax), %ecx
movl 0x20(%rax), %r8d
leaq 0x15209(%rip), %rsi     # 0x2ed37
xorl %eax, %eax
callq 0x2250
movl 0xc(%rsp), %eax
movl %eax, 0x8(%rsp)
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbp, %rdx
jbe 0x19e34
movq (%rax,%rbp,8), %rax
movl 0xc(%rax), %edi
movq 0x8(%r13), %rcx
subq (%r13), %rcx
shrq $0x3, %rcx
decl %ecx
movq %r13, %rsi
xorl %edx, %edx
callq 0x16b4d
testl %eax, %eax
jle 0x19bc4
movq (%r13), %rdi
movq 0x8(%r13), %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
movl %eax, %esi
cmpq %rsi, %rdx
jbe 0x19e1f
movq (%r14), %r8
movq 0x8(%r14), %rcx
subq %r8, %rcx
sarq $0x3, %rcx
cmpq %rbp, %rcx
jbe 0x19e10
movq (%rdi,%rsi,8), %rcx
movl 0x20(%rcx), %ecx
movq (%r8,%rbp,8), %rsi
cmpl 0xc(%rsi), %ecx
jle 0x19bc4
leal -0x1(%rax), %ecx
cmpl $0x1, %eax
movl %ecx, %eax
jg 0x19b86
xorl %eax, %eax
movq (%r13), %rdi
movq 0x8(%r13), %rcx
subq %rdi, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
cmpl %edx, %eax
setl %r10b
jge 0x19ca5
movl 0x8(%rsp), %r8d
cltq
shrq $0x3, %rcx
movslq %ecx, %r9
movq %rax, %rsi
shlq $0x20, %rsi
cmpq %rax, %rdx
jbe 0x19e28
movq (%r14), %r11
movq 0x8(%r14), %rcx
subq %r11, %rcx
sarq $0x3, %rcx
cmpq %rbp, %rcx
jbe 0x19e10
movq (%rdi,%rax,8), %rbx
movl 0xc(%rbx), %r12d
movq (%r11,%rbp,8), %rcx
cmpl 0x20(%rcx), %r12d
jge 0x19c53
movl 0xc(%rcx), %ecx
subl 0x20(%rbx), %ecx
jg 0x19c33
xorl %ecx, %ecx
cmpl %ecx, %r8d
jle 0x19c3f
movl %ecx, 0x8(%rsp)
movl %ecx, %r8d
incq %rax
cmpq %r9, %rax
setl %r10b
addq %r15, %rsi
cmpq %rax, %r9
jne 0x19bf7
jmp 0x19ca5
testb $0x1, %r10b
je 0x19ca5
movq (%r14), %rcx
movq 0x8(%r14), %rax
subq %rcx, %rax
sarq $0x3, %rax
cmpq %rbp, %rax
jbe 0x19e51
sarq $0x20, %rsi
cmpq %rsi, %rdx
jbe 0x19e1f
movq (%rcx,%rbp,8), %rcx
movq (%rdi,%rsi,8), %rdx
movl 0xc(%rcx), %eax
subl 0x20(%rdx), %eax
jg 0x19c9b
movl 0xc(%rdx), %eax
subl 0x20(%rcx), %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
cmpl %eax, 0x8(%rsp)
jle 0x19ca5
movl %eax, 0x8(%rsp)
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbp, %rdx
jbe 0x19e34
movq 0x2031d(%rip), %rcx     # 0x39fe0
movq (%rcx), %rdi
movq (%rax,%rbp,8), %rax
movq (%rax), %rdx
movl 0xc(%rax), %ecx
movl 0x20(%rax), %r8d
movl 0x8(%rsp), %r9d
leaq 0x14459(%rip), %rsi     # 0x2e139
xorl %eax, %eax
callq 0x2250
movq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x19d06
movl 0x8(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x19d10
leaq 0x8(%rsp), %rdx
callq 0xadd0
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 0x19b06
movq 0x30(%rsp), %r12
incq %r12
movq 0x20(%rsp), %rbx
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r12
jl 0x19a6a
jmp 0x19dfc
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r12, %rdx
jbe 0x19e45
movq 0x2025c(%rip), %rcx     # 0x39fe0
movq (%rcx), %rdi
movq (%rax,%r12,8), %rdx
leaq 0x14fbd(%rip), %rsi     # 0x2ed4f
xorl %eax, %eax
callq 0x2250
movq 0x20238(%rip), %rax     # 0x39fd8
movq (%rax), %rdi
callq 0x20a0
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
testl %eax, %eax
jle 0x19d33
xorl %ebp, %ebp
movq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x19ddc
movl 0xc(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x19de6
leaq 0xc(%rsp), %rdx
callq 0xadd0
incl %ebp
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
cmpl %eax, %ebp
jl 0x19dbd
jmp 0x19d33
movq 0x10(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x13abf(%rip), %rdi     # 0x2d8d6
movq %rbp, %rsi
movq %rcx, %rdx
jmp 0x19e3e
leaq 0x13ab0(%rip), %rdi     # 0x2d8d6
jmp 0x19e3e
leaq 0x13aa7(%rip), %rdi     # 0x2d8d6
movq %rax, %rsi
jmp 0x19e3e
leaq 0x13a9b(%rip), %rdi     # 0x2d8d6
movq %rbp, %rsi
xorl %eax, %eax
callq 0x2270
leaq 0x13a8a(%rip), %rdi     # 0x2d8d6
movq %r12, %rsi
jmp 0x19e3e
leaq 0x13a7e(%rip), %rdi     # 0x2d8d6
movq %rbp, %rsi
movq %rax, %rdx
jmp 0x19e3e