// File: /workspace/github_cpp_makefile_star2_O1/gersteinlab[P]MUSIC/src/ms_genome_sequence_tools.cpp
// Function: batch_extract_interval_sequences(char*, std::vector<t_annot_region*, std::allocator<t_annot_region*>>*)
void batch_extract_interval_sequences(char* chr_data_dir, vector<t_annot_region*>* regions)
{
	t_sorted_annot_region_lists* retstr_regions = restructure_annot_regions(regions);

	for(int i_chr = 0; i_chr < (int)retstr_regions->chr_ids->size(); i_chr++)
	{
		fprintf(stderr, "Processing %s\n", retstr_regions->chr_ids->at(i_chr));

		vector<t_annot_region*>* cur_chr_regions = retstr_regions->regions_per_chrom[i_chr];
		for(int i_reg = 0; i_reg < (int)cur_chr_regions->size(); i_reg++)
		{
			// Process the current transcript: Merge the exons
			vector<t_annot_region*>* merged_exons = merge_annot_regions(cur_chr_regions->at(i_reg)->intervals, 0, true);

			// Sort the merged regions.
			sort(merged_exons->begin(), merged_exons->end(), sort_regions);

			t_string* cur_transcript_seq = new t_string();

			// Extract the sequence for all the exons.
			for(int i_ex = 0; i_ex < (int)merged_exons->size(); i_ex++)
			{
				if(merged_exons->at(i_ex)->strand == '-')
				{
					merged_exons->at(i_ex)->strand = '+';
				}

				char* cur_exon_seq = extract_sequence(chr_data_dir, retstr_regions->chr_ids->at(i_chr), merged_exons->at(i_ex)->start, merged_exons->at(i_ex)->end, merged_exons->at(i_ex)->strand);
				cur_transcript_seq->concat_string(cur_exon_seq);
				delete [] cur_exon_seq;
			} // i_ex loop.

			// If the transcript is on negative strand, revert the sequence.
			char* transcript_seq = t_string::copy_me_str(cur_transcript_seq->str());
			if(cur_chr_regions->at(i_reg)->strand == '-')
			{
				//cur_transcript_seq->revert();
				delete [] transcript_seq;
				transcript_seq = get_reverse_complement(cur_transcript_seq->str(), 0, t_string::string_length(cur_transcript_seq->str())-1);
			}

			// Copy the data.
			cur_chr_regions->at(i_reg)->data = transcript_seq;

			delete(cur_transcript_seq);
			// Memory for merged exons must be freeed.
		} // i_reg loop.
	} // i_chr loop.
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r12
movq %rsi, %rdi
callq 0x16742
movq %rax, %r15
movq 0x18(%rax), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x2295b
xorl %ebp, %ebp
movq %r12, 0x20(%rsp)
movq %r15, 0x28(%rsp)
cmpq %rbp, %rdx
jbe 0x22976
movq 0x17a66(%rip), %rcx     # 0x39fe0
movq (%rcx), %rdi
movq (%rax,%rbp,8), %rdx
leaq 0x971b(%rip), %rsi      # 0x2bca3
xorl %eax, %eax
callq 0x2250
movq (%r15), %rax
movq (%rax,%rbp,8), %r14
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x22933
xorl %ebx, %ebx
movq %rbp, 0x30(%rsp)
movq %r14, 0x8(%rsp)
cmpq %rbx, %rdx
jbe 0x2296a
movq %rbx, 0x10(%rsp)
movq (%rax,%rbx,8), %rax
movq 0x30(%rax), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x1ab1c
movq (%rax), %r13
movq %rax, 0x18(%rsp)
movq 0x8(%rax), %r14
cmpq %r14, %r13
je 0x22799
movq %r14, %rbx
subq %r13, %rbx
movq %rbx, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %r14, %rsi
leaq -0xb385(%rip), %rcx     # 0x17291
callq 0x1c869
leaq 0x8(%r13), %rax
cmpq $0x81, %rbx
movq %r14, (%rsp)
jl 0x2271a
movl $0x8, %r14d
movq %rax, 0x38(%rsp)
movq %rax, %rbx
movq %r13, %r15
leaq (%r14,%r13), %r12
movq (%r13,%r14), %rdi
movq (%r13), %rsi
callq 0x17291
movq (%r13,%r14), %rbp
testb %al, %al
movq %r12, 0x40(%rsp)
je 0x22676
movq 0x38(%rsp), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x22b0
movq %r13, %r12
jmp 0x226a6
movq (%r15), %rsi
movq %rbp, %rdi
callq 0x17291
testb %al, %al
je 0x226a6
movq %rbx, %rcx
leaq -0x8(%rcx), %r12
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %rbp, %rdi
callq 0x17291
movq %r12, %rcx
testb %al, %al
jne 0x22688
movq %rbp, (%r12)
addq $0x8, %r14
addq $0x8, %rbx
cmpq $0x80, %r14
movq 0x40(%rsp), %r15
jne 0x22641
subq $-0x80, %r13
cmpq (%rsp), %r13
movq 0x20(%rsp), %r12
je 0x22799
movq -0x8(%r13), %rsi
movq (%r13), %rbx
movq %rbx, %rdi
callq 0x17291
movq %r13, %r14
testb %al, %al
je 0x2270b
movq %r13, %rcx
leaq -0x8(%rcx), %r14
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %rbx, %rdi
callq 0x17291
movq %r14, %rcx
testb %al, %al
jne 0x226ed
movq %rbx, (%r14)
addq $0x8, %r13
cmpq (%rsp), %r13
jne 0x226d3
jmp 0x22799
cmpq %r14, %rax
je 0x22799
movq %r13, %rbp
movq %rbp, %r14
movq %rax, %rbp
movq (%rax), %rdi
movq (%r13), %rsi
callq 0x17291
movq (%rbp), %rbx
testb %al, %al
je 0x22759
movq %rbp, %rdx
subq %r13, %rdx
subq %rdx, %r14
addq $0x10, %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x22b0
movq %r13, %r14
jmp 0x2278c
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x17291
movq %rbp, %r14
testb %al, %al
je 0x2278c
movq %rbp, %rcx
leaq -0x8(%rcx), %r14
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %rbx, %rdi
callq 0x17291
movq %r14, %rcx
testb %al, %al
jne 0x2276e
movq %rbx, (%r14)
leaq 0x8(%rbp), %rax
cmpq (%rsp), %rax
jne 0x22722
movl $0x8, %edi
callq 0x2180
movq %rax, %r13
movq %rax, %rdi
callq 0x26dbc
movq 0x18(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
movq 0x28(%rsp), %r15
movq 0x30(%rsp), %rbp
jle 0x22865
xorl %ebx, %ebx
cmpq %rbx, %rdx
jbe 0x2296a
movq (%rax,%rbx,8), %rax
cmpb $0x2d, 0x8(%rax)
jne 0x227ec
movb $0x2b, 0x8(%rax)
movq 0x18(%r15), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rbp, %rdx
jbe 0x22976
movq (%rcx,%rbp,8), %rsi
movl 0xc(%rax), %edx
movl 0x20(%rax), %ecx
movsbl 0x8(%rax), %r8d
movq %r12, %rdi
xorl %r9d, %r9d
callq 0x2299f
movq %rax, %r14
movq %r13, %rdi
movq %rax, %rsi
callq 0x2931e
testq %r14, %r14
je 0x2283c
movq %r14, %rdi
callq 0x2220
incq %rbx
movq 0x18(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rbx
jl 0x227d5
movq %r13, %rdi
callq 0x275f2
movq %rax, %rdi
callq 0x273e0
movq 0x8(%rsp), %r14
movq (%r14), %rcx
movq 0x8(%r14), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
movq 0x10(%rsp), %rbx
cmpq %rbx, %rdx
jbe 0x2296a
movq (%rcx,%rbx,8), %rcx
cmpb $0x2d, 0x8(%rcx)
jne 0x228df
testq %rax, %rax
je 0x228ad
movq %rax, %rdi
callq 0x2220
movq %r13, %rdi
callq 0x275f2
movq %rax, %rbx
movq %r13, %rdi
callq 0x275f2
movq %rax, %rdi
callq 0x26e82
leal -0x1(%rax), %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x22b98
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r14
movq (%r14), %rcx
movq 0x8(%r14), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0x2296a
movq (%rcx,%rbx,8), %rcx
movq %rax, 0x48(%rcx)
movq %r13, %rdi
callq 0x27708
movl $0x8, %esi
movq %r13, %rdi
callq 0x2190
incq %rbx
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rbx
jl 0x225b8
incq %rbp
movq 0x18(%r15), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rbp
jl 0x2256a
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xaf65(%rip), %rdi      # 0x2d8d6
movq %rbx, %rsi
jmp 0x22980
leaq 0xaf59(%rip), %rdi      # 0x2d8d6
movq %rbp, %rsi
xorl %eax, %eax
callq 0x2270
movq %rax, %rbx
movl $0x8, %esi
movq %r13, %rdi
callq 0x2190
movq %rbx, %rdi
callq 0x22e0