// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movq %rsi, %r15
movq %rdi, %r14
movl $0x989680, 0x4(%rsp)    # imm = 0x989680
movl $0x18, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x8(%rsp)
movq $0x0, 0x10(%rax)
movq %r14, %rdi
callq 0x163f1
movq %rax, %rbx
movq %r15, %rdi
callq 0x163f1
movq %rax, 0x10(%rsp)
movq %rbx, 0x20(%rsp)
movq 0x18(%rbx), %rbx
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x199bf
movabsq $0x100000000, %r15      # imm = 0x100000000
xorl %r12d, %r12d
movq %rbx, 0x18(%rsp)
cmpq %r12, %rdx
jbe 0x19a12
movq 0x1f959(%rip), %rcx     # 0x38fe0
movq (%rcx), %rdi
movq (%rax,%r12,8), %rdx
leaq 0x14688(%rip), %rsi     # 0x2dd1d
xorl %eax, %eax
callq 0x2250
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r12, %rdx
jbe 0x19a12
movq 0x10(%rsp), %rcx
movq 0x18(%rcx), %r13
movq (%rax,%r12,8), %rsi
movq %r13, %rdi
callq 0x26ed0
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
movq (%rcx,%r12,8), %r14
movq 0x8(%r13), %rcx
subq (%r13), %rcx
shrq $0x3, %rcx
cmpl %ecx, %eax
jge 0x1991d
movq (%r14), %rcx
movq 0x8(%r14), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x198ef
movq 0x10(%rsp), %rsi
movq (%rsi), %rsi
cltq
movq (%rsi,%rax,8), %r13
xorl %ebp, %ebp
cmpq %rbp, %rdx
jbe 0x19a06
movq 0x1f8c2(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
movq (%rcx,%rbp,8), %rax
movq (%rax), %rdx
movl 0xc(%rax), %ecx
movl 0x20(%rax), %r8d
leaq 0x14601(%rip), %rsi     # 0x2dd37
xorl %eax, %eax
callq 0x2250
movl 0x4(%rsp), %eax
movl %eax, (%rsp)
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbp, %rdx
jbe 0x19a06
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 0x167a8
movq (%r13), %rdi
movq 0x8(%r13), %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
testl %eax, %eax
jle 0x197cb
movl %eax, %esi
cmpq %rsi, %rdx
jbe 0x199e2
movq (%r14), %r8
movq 0x8(%r14), %rcx
subq %r8, %rcx
sarq $0x3, %rcx
cmpq %rbp, %rcx
jbe 0x199eb
movq (%rdi,%rsi,8), %rcx
movl 0x20(%rcx), %ecx
movq (%r8,%rbp,8), %rsi
cmpl 0xc(%rsi), %ecx
jle 0x197cb
leal -0x1(%rax), %ecx
cmpl $0x1, %eax
movl %ecx, %eax
jg 0x1978d
xorl %eax, %eax
movl (%rsp), %r9d
cmpl %edx, %eax
jge 0x19834
movslq %eax, %rcx
movq %rcx, %rsi
shlq $0x20, %rsi
cmpq %rcx, %rdx
jbe 0x199fa
movq (%r14), %r8
movq 0x8(%r14), %rax
subq %r8, %rax
sarq $0x3, %rax
cmpq %rbp, %rax
jbe 0x199d3
movq (%rdi,%rcx,8), %rbx
movq (%r8,%rbp,8), %r11
movl 0x20(%r11), %r10d
cmpl %r10d, 0xc(%rbx)
jge 0x19844
movl 0xc(%r11), %r10d
subl 0x20(%rbx), %r10d
jg 0x1981c
xorl %r10d, %r10d
cmpl %r10d, %r9d
jle 0x19828
movl %r10d, (%rsp)
movl %r10d, %r9d
incq %rcx
addq %r15, %rsi
cmpl %ecx, %edx
jne 0x197dd
jmp 0x19877
movq (%r14), %r8
movq 0x8(%r14), %rax
subq %r8, %rax
sarq $0x3, %rax
jmp 0x19877
sarq $0x20, %rsi
cmpq %rsi, %rdx
jbe 0x199e2
movq (%rdi,%rsi,8), %rdx
movl 0xc(%r11), %ecx
subl 0x20(%rdx), %ecx
jg 0x1986c
movl 0xc(%rdx), %ecx
subl %r10d, %ecx
movl $0x0, %edx
cmovlel %edx, %ecx
cmpl %ecx, %r9d
jle 0x19877
movl %ecx, (%rsp)
movl %ecx, %r9d
cmpq %rbp, %rax
jbe 0x199d3
movq 0x1f759(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
movq (%r8,%rbp,8), %rax
movq (%rax), %rdx
movl 0xc(%rax), %ecx
movl 0x20(%rax), %r8d
leaq 0x1389a(%rip), %rsi     # 0x2d139
xorl %eax, %eax
callq 0x2250
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x198c4
movl (%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x198cc
movq %rsp, %rdx
callq 0xabfa
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 0x1970e
incq %r12
movq 0x18(%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 0x19677
jmp 0x199bf
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r12, %rdx
jbe 0x19a12
movq 0x1f6a5(%rip), %rcx     # 0x38fe0
movq (%rcx), %rdi
movq (%rax,%r12,8), %rdx
leaq 0x14406(%rip), %rsi     # 0x2dd4f
xorl %eax, %eax
callq 0x2250
movq 0x1f681(%rip), %rax     # 0x38fd8
movq (%rax), %rdi
callq 0x20a0
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq %rcx, %rdx
subq %rax, %rdx
shrq $0x3, %rdx
testl %edx, %edx
jle 0x198ef
xorl %ebp, %ebp
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x19999
movl 0x4(%rsp), %edx
movl %edx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x199aa
leaq 0x4(%rsp), %rdx
callq 0xabfa
movq (%r14), %rax
movq 0x8(%r14), %rcx
incl %ebp
movq %rcx, %rdx
subq %rax, %rdx
shrq $0x3, %rdx
cmpl %edx, %ebp
jl 0x1997a
jmp 0x198ef
movq 0x8(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x12ef6(%rip), %rdi     # 0x2c8d0
movq %rbp, %rsi
movq %rax, %rdx
jmp 0x19a1c
leaq 0x12ee7(%rip), %rdi     # 0x2c8d0
jmp 0x19a1c
leaq 0x12ede(%rip), %rdi     # 0x2c8d0
movq %rbp, %rsi
movq %rcx, %rdx
jmp 0x19a1c
leaq 0x12ecf(%rip), %rdi     # 0x2c8d0
movq %rcx, %rsi
jmp 0x19a1c
leaq 0x12ec3(%rip), %rdi     # 0x2c8d0
movq %rbp, %rsi
jmp 0x19a1c
leaq 0x12eb7(%rip), %rdi     # 0x2c8d0
movq %r12, %rsi
xorl %eax, %eax
callq 0x2270