// File: /workspace/github_cpp_makefile_star2_O2/gersteinlab[P]MUSIC/src/ms_annot_region_tools.cpp
// Function: restructure_annot_regions(std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, std::vector<char*, std::allocator<char*>>*)
t_sorted_annot_region_lists* restructure_annot_regions(vector<t_annot_region*>* regions, vector<char*>* chr_ids)
{
	// This is the list of regions per chromosome.
	t_sorted_annot_region_lists* region_lists = new t_sorted_annot_region_lists();

	region_lists->chr_ids = NULL; // Do not free the memory if it is null.
	region_lists->neg_strand_regions_per_chrom = new vector<t_annot_region*>*[(int)chr_ids->size() + 2];
	region_lists->pos_strand_regions_per_chrom = new vector<t_annot_region*>*[(int)chr_ids->size() + 2];
	region_lists->regions_per_chrom = new vector<t_annot_region*>*[(int)chr_ids->size() + 2];

	for(int i_chr = 0; i_chr < (int)chr_ids->size(); i_chr++)
	{
		region_lists->pos_strand_regions_per_chrom[i_chr] = new vector<t_annot_region*>();
		region_lists->neg_strand_regions_per_chrom[i_chr] = new vector<t_annot_region*>();
		region_lists->regions_per_chrom[i_chr] = new vector<t_annot_region*>();
	} // i_chr loop.

	// Go over all the regions and separate them into appropriate list.
	for(int i_reg = 0; i_reg < (int)regions->size(); i_reg++)
	{
		int i_chr = t_string::get_i_str(chr_ids, regions->at(i_reg)->chrom);

		if(i_chr < (int)chr_ids->size())
		{
			region_lists->regions_per_chrom[i_chr]->push_back(regions->at(i_reg));

			if(regions->at(i_reg)->strand == '+')
			{
				region_lists->pos_strand_regions_per_chrom[i_chr]->push_back(regions->at(i_reg));
			}
			else if(regions->at(i_reg)->strand == '-')
			{
				region_lists->neg_strand_regions_per_chrom[i_chr]->push_back(regions->at(i_reg));
			}
			else
			{
				printf("Unknown strand char %c @ %s(%d).\n", regions->at(i_reg)->strand, __FILE__, __LINE__);
				exit(0);
			}
		}
	} // i_reg loop.

	for(int i_chr = 0; i_chr < (int)chr_ids->size(); i_chr++)
	{
		region_lists->pos_strand_regions_per_chrom[i_chr] = new vector<t_annot_region*>();
		region_lists->neg_strand_regions_per_chrom[i_chr] = new vector<t_annot_region*>();
		region_lists->regions_per_chrom[i_chr] = new vector<t_annot_region*>();
	} // i_chr loop.

	// Finally sort the regions.
	return(region_lists);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r13
movq %rdi, %rbp
pushq $0x20
popq %rdi
callq 0x2170
movq %rax, %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movq 0x8(%r13), %r14
subq (%r13), %r14
movq %r14, %rax
shlq $0x1d, %rax
movabsq $0x200000000, %rcx      # imm = 0x200000000
addq %rax, %rcx
movq %rcx, %rax
sarq $0x1d, %rax
testq %rcx, %rcx
pushq $-0x1
popq %rbx
cmovnsq %rax, %rbx
movq %rbx, %rdi
callq 0x2040
movq %rax, (%rsp)
movq %rax, 0x10(%r15)
movq %rbx, %rdi
callq 0x2040
movq %rax, 0x8(%rsp)
movq %rax, 0x8(%r15)
movq %rbx, %rdi
callq 0x2040
movq %rax, %r12
movq %r15, 0x10(%rsp)
movq %rax, (%r15)
shrq $0x3, %r14
xorl %r15d, %r15d
testl %r14d, %r14d
cmovlel %r15d, %r14d
pushq $0x18
popq %rbx
cmpq %r15, %r14
je 0x13985
movq %rbx, %rdi
callq 0x2170
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq 0x8(%rsp), %rcx
movq %rax, (%rcx,%r15,8)
movq %rbx, %rdi
callq 0x2170
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq (%rsp), %rcx
movq %rax, (%rcx,%r15,8)
movq %rbx, %rdi
callq 0x2170
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, (%r12,%r15,8)
incq %r15
jmp 0x1392d
xorl %ebx, %ebx
movq 0x8(%rbp), %rax
subq (%rbp), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbx
jge 0x13a3b
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
movq (%rax), %rsi
movq %r13, %rdi
callq 0x21152
movq 0x8(%r13), %rcx
subq (%r13), %rcx
shrq $0x3, %rcx
cmpl %ecx, %eax
jge 0x13a33
movslq %eax, %r15
movq (%r12,%r15,8), %r14
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x7404
movq %r14, %rdi
movq %rax, %rsi
callq 0x7448
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
cmpb $0x2b, 0x8(%rax)
movq 0x8(%rsp), %rax
je 0x13a19
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
cmpb $0x2d, 0x8(%rax)
movq (%rsp), %rax
jne 0x13ac0
movq (%rax,%r15,8), %r14
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x7404
movq %r14, %rdi
movq %rax, %rsi
callq 0x7448
incq %rbx
jmp 0x13987
movq 0x8(%r13), %r14
subq (%r13), %r14
shrq $0x3, %r14
xorl %r13d, %r13d
testl %r14d, %r14d
cmovlel %r13d, %r14d
pushq $0x18
popq %rbx
cmpq %r13, %r14
je 0x13aac
movq %rbx, %rdi
callq 0x2170
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq 0x8(%rsp), %rcx
movq %rax, (%rcx,%r13,8)
movq %rbx, %rdi
callq 0x2170
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq (%rsp), %rcx
movq %rax, (%rcx,%r13,8)
movq %rbx, %rdi
callq 0x2170
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, (%r12,%r13,8)
incq %r13
jmp 0x13a54
movq 0x10(%rsp), %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
movsbl 0x8(%rax), %esi
leaq 0x13ee8(%rip), %rdi     # 0x279c1
leaq 0x13f03(%rip), %rdx     # 0x279e3
movl $0x278, %ecx            # imm = 0x278
xorl %eax, %eax
callq 0x2050
xorl %edi, %edi
callq 0x21d0