// File: 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:
subq $0x98, %rsp
movq %rdi, 0x90(%rsp)
movq %rsi, 0x88(%rsp)
movl $0x20, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x68(%rsp)
xorl %esi, %esi
movl $0x20, %edx
callq 0x2110
movq 0x68(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rax
movq $0x0, 0x18(%rax)
movq 0x88(%rsp), %rdi
callq 0xc220
addl $0x2, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, %rcx
movq 0x80(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x88(%rsp), %rdi
callq 0xc220
addl $0x2, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, %rcx
movq 0x80(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x88(%rsp), %rdi
callq 0xc220
addl $0x2, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, %rcx
movq 0x80(%rsp), %rax
movq %rcx, (%rax)
movl $0x0, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
movl %eax, 0x64(%rsp)
movq 0x88(%rsp), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x64(%rsp), %eax
cmpl %ecx, %eax
jge 0x2acb8
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x48(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x48(%rsp), %rdi
callq 0xc670
movq 0x48(%rsp), %rdx
movq 0x80(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x7c(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x50(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x50(%rsp), %rdi
callq 0xc670
movq 0x50(%rsp), %rdx
movq 0x80(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x7c(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x58(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x58(%rsp), %rdi
callq 0xc670
movq 0x58(%rsp), %rdx
movq 0x80(%rsp), %rax
movq (%rax), %rax
movslq 0x7c(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x2abbf
movl $0x0, 0x78(%rsp)
movl 0x78(%rsp), %eax
movl %eax, 0x44(%rsp)
movq 0x90(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0x44(%rsp), %eax
cmpl %ecx, %eax
jge 0x2ae78
movq 0x88(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x90(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq 0x38(%rsp), %rdi
movq (%rax), %rax
movq (%rax), %rsi
callq 0x47cf0
movl %eax, 0x74(%rsp)
movl 0x74(%rsp), %eax
movl %eax, 0x40(%rsp)
movq 0x88(%rsp), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x40(%rsp), %eax
cmpl %ecx, %eax
jge 0x2ae66
movq 0x80(%rsp), %rax
movq (%rax), %rax
movslq 0x74(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x30(%rsp)
movq 0x90(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0xc8c0
movq 0x90(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movsbl 0x8(%rax), %eax
cmpl $0x2b, %eax
jne 0x2adcf
movq 0x80(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x74(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x28(%rsp)
movq 0x90(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0xc8c0
jmp 0x2ae64
movq 0x90(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movsbl 0x8(%rax), %eax
cmpl $0x2d, %eax
jne 0x2ae28
movq 0x80(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x74(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x20(%rsp)
movq 0x90(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0xc8c0
jmp 0x2ae62
movq 0x90(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movsbl 0x8(%rax), %esi
leaq 0x2af39(%rip), %rdi     # 0x55d81
leaq 0x2af54(%rip), %rdx     # 0x55da3
movl $0x278, %ecx            # imm = 0x278
movb $0x0, %al
callq 0x2050
xorl %edi, %edi
callq 0x21f0
jmp 0x2ae64
jmp 0x2ae66
jmp 0x2ae68
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x2acc0
movl $0x0, 0x70(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x88(%rsp), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0x2af76
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, (%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq (%rsp), %rdi
callq 0xc670
movq (%rsp), %rdx
movq 0x80(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x70(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x8(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x8(%rsp), %rdi
callq 0xc670
movq 0x8(%rsp), %rdx
movq 0x80(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x70(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x10(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x10(%rsp), %rdi
callq 0xc670
movq 0x10(%rsp), %rdx
movq 0x80(%rsp), %rax
movq (%rax), %rax
movslq 0x70(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x70(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x70(%rsp)
jmp 0x2ae80
movq 0x80(%rsp), %rax
addq $0x98, %rsp
retq
nopw %cs:(%rax,%rax)