// File: /workspace/github_cpp_makefile_star2_O2/gersteinlab[P]MUSIC/src/ms_annot_region_tools.cpp
// Function: extract_region_ends_by_fraction_extension(std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, double, bool, bool)
vector<t_annot_region*>* extract_region_ends_by_fraction_extension(vector<t_annot_region*>* regions, double ext_fraction,
	bool extract_5p, 
	bool extract_3p)
{
	vector<t_annot_region*>* region_ends = new vector<t_annot_region*>();
	for(int i = 0; i < (int)regions->size(); i++)
	{
		int l_ext = (int)(ext_fraction * (regions->at(i)->end - regions->at(i)->start + 1));

		if(extract_5p)
		{
			t_annot_region* fp_end = get_empty_region();
			if(regions->at(i)->strand == '+')
			{
				fp_end->chrom = t_string::copy_me_str(regions->at(i)->chrom);
				fp_end->start = (regions->at(i)->start > l_ext)?(regions->at(i)->start - l_ext):(1);
				//fp_end->end = (regions->at(i)->start + l_ext);
				fp_end->end = (regions->at(i)->start);
				fp_end->strand = regions->at(i)->strand;
			}
			else
			{
				fp_end->chrom = t_string::copy_me_str(regions->at(i)->chrom);
				//fp_end->start = (regions->at(i)->end > l_ext)?(regions->at(i)->end - l_ext):(1);
				fp_end->start = regions->at(i)->end;
				fp_end->end = (regions->at(i)->end + l_ext);
				fp_end->strand = regions->at(i)->strand;
			}
			region_ends->push_back(fp_end);
		} // 5p extraction check.

		if(extract_3p)
		{
			t_annot_region* tp_end = get_empty_region();
			if(regions->at(i)->strand == '-')
			{
				tp_end->chrom = t_string::copy_me_str(regions->at(i)->chrom);
				tp_end->start = (regions->at(i)->start > l_ext)?(regions->at(i)->start - l_ext):(1);
				//tp_end->end = (regions->at(i)->start + l_ext);
				tp_end->end = regions->at(i)->start;
				tp_end->strand = regions->at(i)->strand;
			}
			else
			{
				tp_end->chrom = t_string::copy_me_str(regions->at(i)->chrom);
				//tp_end->start = (regions->at(i)->end > l_ext)?(regions->at(i)->end - l_ext):(1);
				tp_end->start = regions->at(i)->end;
				tp_end->end = (regions->at(i)->end + l_ext);
				tp_end->strand = regions->at(i)->strand;
			}
			region_ends->push_back(tp_end);
		} // 3p extraction check.
	} // i loop.

	return(region_ends);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, 0xc(%rsp)
movl %esi, %ebp
movsd %xmm0, 0x10(%rsp)
movq %rdi, %r14
pushq $0x18
popq %rdi
callq 0x2170
movq %rax, %r15
xorpd %xmm0, %xmm0
andq $0x0, 0x10(%rax)
movupd %xmm0, (%rax)
xorl %r12d, %r12d
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r12
jge 0x122e0
movq %r14, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %r13d
movq %r14, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %eax
negl %eax
addl %r13d, %eax
incl %eax
cvtsi2sd %eax, %xmm0
mulsd 0x10(%rsp), %xmm0
cvttsd2si %xmm0, %r13d
testb %bpl, %bpl
je 0x12204
callq 0x11e71
movq %rax, (%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movb 0x8(%rax), %bl
movq %r14, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movq (%rax), %rdi
callq 0x21062
movq (%rsp), %rcx
movq %rax, (%rcx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rcx
cmpb $0x2b, %bl
jne 0x121bc
pushq $0x1
popq %rax
cmpl %r13d, 0xc(%rcx)
jle 0x121a2
movq %r14, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %eax
subl %r13d, %eax
movq (%rsp), %rcx
movl %eax, 0xc(%rcx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %eax
jmp 0x121da
movl 0x20(%rcx), %eax
movq (%rsp), %rcx
movl %eax, 0xc(%rcx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %eax
addl %r13d, %eax
movq (%rsp), %rcx
movl %eax, 0x20(%rcx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movb 0x8(%rax), %al
movq (%rsp), %rcx
movb %al, 0x8(%rcx)
movq %r15, %rdi
movq %rsp, %rsi
callq 0x7448
cmpb $0x0, 0xc(%rsp)
je 0x122d8
callq 0x11e71
movq %rax, (%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movb 0x8(%rax), %bl
movq %r14, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movq (%rax), %rdi
callq 0x21062
movq (%rsp), %rcx
movq %rax, (%rcx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rcx
cmpb $0x2d, %bl
jne 0x12291
pushq $0x1
popq %rax
cmpl %r13d, 0xc(%rcx)
jle 0x12276
movq %r14, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %eax
subl %r13d, %eax
movq (%rsp), %rcx
movl %eax, 0xc(%rcx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %r13d
jmp 0x122ad
movl 0x20(%rcx), %eax
movq (%rsp), %rcx
movl %eax, 0xc(%rcx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
addl 0x20(%rax), %r13d
movq (%rsp), %rax
movl %r13d, 0x20(%rax)
movq %r14, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movb 0x8(%rax), %al
movq (%rsp), %rcx
movb %al, 0x8(%rcx)
movq %r15, %rdi
movq %rsp, %rsi
callq 0x7448
incq %r12
jmp 0x120e3
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq