// File: /workspace/github_cpp_makefile_star2_O1/gersteinlab[P]MUSIC/src/ms_annot_region_tools.cpp
// Function: restructure_annot_regions(std::vector<t_annot_region*, std::allocator<t_annot_region*>>*)
t_sorted_annot_region_lists* restructure_annot_regions(vector<t_annot_region*>* regions)
{
	// This is the list of regions per chromosome.
	vector<char*>* chr_ids = get_chr_ids(regions);
	t_sorted_annot_region_lists* region_lists = new t_sorted_annot_region_lists();

	// The chromosome id list is built up dynamically in the region list loop.
	//region_lists->chr_ids = new vector<char*>();
	region_lists->chr_ids = chr_ids;
	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.

	// Sort the regions per chromosome and all the regions.
	for(int i_chr = 0; i_chr < (int)chr_ids->size(); i_chr++)
	{
		sort(region_lists->pos_strand_regions_per_chrom[i_chr]->begin(), region_lists->pos_strand_regions_per_chrom[i_chr]->end(), sort_regions);
		sort(region_lists->neg_strand_regions_per_chrom[i_chr]->begin(), region_lists->neg_strand_regions_per_chrom[i_chr]->end(), sort_regions);
		sort(region_lists->regions_per_chrom[i_chr]->begin(), region_lists->regions_per_chrom[i_chr]->end(), sort_regions);
	} // i_chr loop.

	// Finally sort the regions.
	return(region_lists);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, 0x8(%rsp)
callq 0x1756e
movq %rax, %rbx
movl $0x20, %edi
callq 0x2180
movq %rax, %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq %rbx, 0x18(%rax)
movq 0x8(%rbx), %rax
subq (%rbx), %rax
shlq $0x1d, %rax
movabsq $0x200000000, %r13      # imm = 0x200000000
addq %r13, %rax
movq %rax, %rdi
sarq $0x1d, %rdi
testq %rax, %rax
movq $-0x1, %r15
cmovsq %r15, %rdi
callq 0x2040
movq %rax, %r14
movq %rax, 0x10(%r12)
movq 0x8(%rbx), %rax
subq (%rbx), %rax
shlq $0x1d, %rax
addq %r13, %rax
movq %rax, %rdi
sarq $0x1d, %rdi
testq %rax, %rax
cmovsq %r15, %rdi
callq 0x2040
movq %rax, 0x8(%r12)
movq 0x8(%rbx), %rcx
subq (%rbx), %rcx
shlq $0x1d, %rcx
addq %r13, %rcx
movq %rax, %r13
movq %rcx, %rdi
sarq $0x1d, %rdi
testq %rcx, %rcx
cmovsq %r15, %rdi
callq 0x2040
movq %rax, %rbp
movq %r12, 0x18(%rsp)
movq %rax, (%r12)
movq %r14, %r12
movq 0x8(%rbx), %rax
subq (%rbx), %rax
shrq $0x3, %rax
testl %eax, %eax
jle 0x1687f
xorl %r14d, %r14d
xorl %r15d, %r15d
movl $0x18, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %r14, 0x10(%rax)
movq %rax, (%r13,%r15,8)
movl $0x18, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %r14, 0x10(%rax)
movq %rax, (%r12,%r15,8)
movl $0x18, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %r14, 0x10(%rax)
movq %rax, (%rbp,%r15,8)
movq 0x8(%rbx), %rax
subq (%rbx), %rax
incq %r15
shrq $0x3, %rax
cltq
cmpq %rax, %r15
jl 0x16820
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
movq %rbp, 0x10(%rsp)
jle 0x169c6
xorl %r15d, %r15d
xorl %r14d, %r14d
cmpq %r14, %rdx
jbe 0x16ae9
movq (%rax,%r14,8), %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x274fc
movq 0x8(%rbx), %rcx
subq (%rbx), %rcx
shrq $0x3, %rcx
cmpl %ecx, %eax
jge 0x16999
movq 0x8(%rsp), %rcx
movq (%rcx), %rdx
movq 0x8(%rcx), %rcx
subq %rdx, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
jbe 0x16af5
movq %r13, %rbp
movq %r12, %r13
movslq %eax, %r12
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %rdi
addq %r15, %rdx
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x16918
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x1691d
callq 0xb976
movq 0x8(%rsp), %rax
movq (%rax), %rdx
movq 0x8(%rax), %rax
subq %rdx, %rax
sarq $0x3, %rax
cmpq %r14, %rax
jbe 0x16b04
movq (%rdx,%r15), %rax
addq %r15, %rdx
movzbl 0x8(%rax), %ecx
cmpl $0x2d, %ecx
je 0x1696b
cmpl $0x2b, %ecx
jne 0x16b18
movq (%rbp,%r12,8), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x1698e
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x16993
movq (%r13,%r12,8), %rdi
movq %r13, %r12
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x16987
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x16996
callq 0xb976
jmp 0x16996
callq 0xb976
movq %r13, %r12
movq %rbp, %r13
incq %r14
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
addq $0x8, %r15
cmpq %rcx, %r14
jl 0x168a5
movq %r12, 0x20(%rsp)
movq 0x8(%rbx), %rax
subq (%rbx), %rax
shrq $0x3, %rax
testl %eax, %eax
jle 0x16ad5
xorl %r12d, %r12d
leaq 0x8a9(%rip), %r14       # 0x17291
movq (%r13,%r12,8), %rax
movq (%rax), %rbp
movq 0x8(%rax), %r15
cmpq %r15, %rbp
je 0x16a2c
movq %r15, %rax
subq %rbp, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rcx
callq 0x1c869
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1c99a
movq 0x20(%rsp), %rax
movq (%rax,%r12,8), %rax
movq (%rax), %r15
movq 0x8(%rax), %rbp
cmpq %rbp, %r15
je 0x16a74
movq %rbp, %rax
subq %r15, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rcx
callq 0x1c869
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1c99a
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %rax
movq (%rax), %r15
movq 0x8(%rax), %rbp
cmpq %rbp, %r15
je 0x16abc
movq %rbp, %rax
subq %r15, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rcx
callq 0x1c869
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1c99a
movq 0x8(%rbx), %rax
subq (%rbx), %rax
incq %r12
shrq $0x3, %rax
cltq
cmpq %rax, %r12
jl 0x169e8
movq 0x18(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x16de6(%rip), %rdi     # 0x2d8d6
movq %r14, %rsi
jmp 0x16b11
leaq 0x16dda(%rip), %rdi     # 0x2d8d6
movq %r14, %rsi
movq %rcx, %rdx
jmp 0x16b11
leaq 0x16dcb(%rip), %rdi     # 0x2d8d6
movq %r14, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2270
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x10b56
movq (%rax), %rax
movsbl 0x8(%rax), %esi
leaq 0x17e8e(%rip), %rdi     # 0x2e9c1
leaq 0x17ea9(%rip), %rdx     # 0x2e9e3
movl $0x241, %ecx            # imm = 0x241
xorl %eax, %eax
callq 0x2050
xorl %edi, %edi
callq 0x21e0