// File: /workspace/github_cpp_makefile_star2_O1/nanjiangshu[P]calSOV/calSOV.cpp
// Function: CalSOV(char*, char*, int, double*, int*, double*, int)
int CalSOV(char *obsSec, char *predSec, int seqLength, double *numCorrectHSR, int *numHSR, double *sovHSR, int numState = 3)/*{{{*/
{
    list <list <pair <int, int> > > segment_obs;
    list <list <pair <int, int> > > segment_pred;
    list <list <pair <int, int> > >::iterator it_sl_1;
    list <list <pair <int, int> > >::iterator it_sl_2;
    list <pair <int, int> > ::iterator it_seg_1;
    list <pair <int, int> > ::iterator it_seg_2;
    int i = 0;

    for (i = 0; i < 3; i ++) /*iterator for secondary structure states*/
    {
        list <pair <int, int> > segList;
        segList = GetSecSeg(obsSec, HSRalphabet[i], seqLength);
        segment_obs.push_back(segList);
        segList = GetSecSeg(predSec, HSRalphabet[i], seqLength);
        segment_pred.push_back(segList);
    }
    vector <int> N(3,0); /*Normalization value*/
    vector <double> sum_un_norm(3,0.0); /*sum of un-normalized score*/
    int total_N = 0;
    double total_sum_un_norm = 0.0;

    int b1, e1, b2, e2, minov, maxov, delta, len_seg1, len_seg2;
    double score;
    for (i = 0; i < 3; i ++) /*iterator for secondary structure states*/
    {
        /*For all segments of the observed secondary structure*/
        it_sl_1 = segment_obs.begin();
        advance( it_sl_1, i );

        it_sl_2 = segment_pred.begin();
        advance( it_sl_2, i );

        for (it_seg_1 = (*it_sl_1).begin(); it_seg_1 != (*it_sl_1).end(); ++it_seg_1)
        {
            b1 = (*it_seg_1).first;
            e1 = (*it_seg_1).second;
            len_seg1 = e1-b1;
            bool isOverlap = false;
            for (it_seg_2 = (*it_sl_2).begin(); it_seg_2 != (*it_sl_2).end(); ++it_seg_2)
            {
                b2 = (*it_seg_2).first;
                e2 = (*it_seg_2).second;
                len_seg2 = e2-b2;
                minov = coverage(b1, e1, b2, e2);
                if (minov > 0) {
                    maxov = max(e1,e2) - min(b1,b2);
                } else {
                    maxov = 0;
                }
                if (minov > 0)
                {
                    isOverlap = true;
                    double min1 = min(maxov - minov, minov);
                    double min2 = min(int(len_seg1/2), int(len_seg2/2));
                    delta = min(min1, min2);
                    score = (minov + delta)/double(maxov)*len_seg1;
                    sum_un_norm[i] += score;
                    N[i] += len_seg1;
                }
            }
            if (isOverlap == false) {
                N[i] += len_seg1;
            }
        }
        sovHSR[i] = sum_un_norm[i]/N[i];
        total_sum_un_norm += sum_un_norm[i];
        total_N += N[i];
        numHSR[i] = N[i];
    }
    sovHSR[numState] = total_sum_un_norm/total_N;
    numHSR[numState] = total_N;

   return numHSR[numState];

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, 0x18(%rsp)
movq %r8, 0x20(%rsp)
movl %edx, %ebp
movq %rsi, (%rsp)
movq %rdi, 0x8(%rsp)
leaq 0x38(%rsp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
xorl %r13d, %r13d
movq %r13, 0x10(%rax)
leaq 0x78(%rsp), %r14
movq %r14, 0x8(%r14)
movq %r14, (%r14)
movq %r13, 0x10(%r14)
leaq 0x60(%rsp), %r15
leaq 0x90(%rsp), %r12
movq %r15, 0x68(%rsp)
movq %r15, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
leaq 0x7933(%rip), %rax      # 0xb164
movsbl (%r13,%rax), %edx
movq %r12, %rdi
movq 0x8(%rsp), %rsi
movl %ebp, %ecx
callq 0x36ee
movq %r15, %rdi
movq %r12, %rsi
callq 0x6572
movq 0x90(%rsp), %rdi
cmpq %r12, %rdi
je 0x3873
movq (%rdi), %rbx
movl $0x18, %esi
callq 0x2180
movq %rbx, %rdi
cmpq %r12, %rbx
jne 0x385e
leaq 0x38(%rsp), %rdi
movq %r15, %rsi
callq 0x660a
movq %rax, %rdi
leaq 0x38(%rsp), %rsi
callq 0x2050
incq 0x48(%rsp)
leaq 0x78cb(%rip), %rax      # 0xb164
movsbl (%r13,%rax), %edx
movq %r12, %rdi
movq (%rsp), %rsi
movl %ebp, %ecx
callq 0x36ee
movq %r15, %rdi
movq %r12, %rsi
callq 0x6572
movq 0x90(%rsp), %rdi
cmpq %r12, %rdi
je 0x38da
movq (%rdi), %rbx
movl $0x18, %esi
callq 0x2180
movq %rbx, %rdi
cmpq %r12, %rbx
jne 0x38c5
movq %r14, %rdi
movq %r15, %rsi
callq 0x660a
movq %rax, %rdi
movq %r14, %rsi
callq 0x2050
incq 0x88(%rsp)
movq 0x60(%rsp), %rdi
cmpq %r15, %rdi
je 0x3917
movq (%rdi), %rbx
movl $0x18, %esi
callq 0x2180
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x3902
incq %r13
cmpq $0x3, %r13
jne 0x3817
movl $0xc, %edi
callq 0x2170
movq %rax, %r15
movl $0x0, 0x8(%rax)
movq $0x0, (%rax)
movl $0x18, %edi
callq 0x2170
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x38(%rsp), %rcx
movq %rcx, 0x30(%rsp)
xorpd %xmm0, %xmm0
xorl %edx, %edx
movq 0x78(%rsp), %rcx
movq %rcx, 0x28(%rsp)
xorl %edi, %edi
xorl %ebx, %ebx
movq 0x30(%rsp), %r8
testq %rdi, %rdi
je 0x3991
movq %rdx, %rcx
movq 0x30(%rsp), %r8
movq (%r8), %r8
incq %rcx
jne 0x3989
movl %ebx, 0x14(%rsp)
movq 0x28(%rsp), %r9
testq %rdi, %rdi
je 0x39af
movq %rdx, %rcx
movq 0x28(%rsp), %r9
movq (%r9), %r9
incq %rcx
jne 0x39a7
movq %rdx, 0x50(%rsp)
movq 0x10(%r8), %rcx
addq $0x10, %r8
cmpq %r8, %rcx
je 0x3ae0
movq 0x10(%r9), %r11
addq $0x10, %r9
movq %r8, (%rsp)
movq %r11, 0x58(%rsp)
movl 0x10(%rcx), %r14d
movl 0x14(%rcx), %ebx
movl %ebx, %esi
subl %r14d, %esi
cmpq %r9, %r11
je 0x3ad0
movq %rcx, 0x8(%rsp)
movq %r15, %r10
movl %esi, %r13d
shrl $0x1f, %r13d
addl %esi, %r13d
sarl %r13d
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
xorl %ebp, %ebp
movl 0x10(%r11), %r15d
movl 0x14(%r11), %edx
cmpl %ebx, %edx
movl %ebx, %r12d
cmovll %edx, %r12d
cmpl %r15d, %r14d
movl %r15d, %r8d
cmovgl %r14d, %r8d
movl $0x0, %ecx
subl %r8d, %r12d
jle 0x3a42
cmpl %edx, %ebx
movl %edx, %ecx
cmovgl %ebx, %ecx
cmpl %r14d, %r15d
movl %r14d, %r8d
cmovll %r15d, %r8d
subl %r8d, %ecx
testl %r12d, %r12d
jle 0x3aad
subl %r15d, %edx
movl %ecx, %r8d
subl %r12d, %r8d
cmpl %r8d, %r12d
cmovll %r12d, %r8d
xorps %xmm2, %xmm2
cvtsi2sd %r8d, %xmm2
movl %edx, %r8d
shrl $0x1f, %r8d
addl %edx, %r8d
sarl %r8d
cmpl %r13d, %r8d
cmovgel %r13d, %r8d
xorps %xmm3, %xmm3
cvtsi2sd %r8d, %xmm3
minsd %xmm2, %xmm3
cvttsd2si %xmm3, %edx
addl %r12d, %edx
xorps %xmm2, %xmm2
cvtsi2sd %edx, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %ecx, %xmm3
divsd %xmm3, %xmm2
mulsd %xmm1, %xmm2
addsd (%rax,%rdi,8), %xmm2
movsd %xmm2, (%rax,%rdi,8)
addl %esi, (%r10,%rdi,4)
movb $0x1, %bpl
movq (%r11), %r11
cmpq %r9, %r11
jne 0x3a09
testb $0x1, %bpl
movq %r10, %r15
movq (%rsp), %r8
movq 0x8(%rsp), %rcx
movq 0x58(%rsp), %r11
jne 0x3ad4
addl %esi, (%r15,%rdi,4)
movq (%rcx), %rcx
cmpq %r8, %rcx
jne 0x39d6
movsd (%rax,%rdi,8), %xmm1
movl (%r15,%rdi,4), %ecx
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
addsd %xmm1, %xmm0
divsd %xmm2, %xmm1
movq 0x18(%rsp), %rdx
movsd %xmm1, (%rdx,%rdi,8)
movl 0x14(%rsp), %ebx
addl %ecx, %ebx
movq 0x20(%rsp), %rdx
movl %ecx, (%rdx,%rdi,4)
incq %rdi
movq 0x50(%rsp), %rdx
decq %rdx
cmpq $0x3, %rdi
jne 0x3977
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
divsd %xmm1, %xmm0
movslq 0xe0(%rsp), %rcx
movq 0x18(%rsp), %rdx
movsd %xmm0, (%rdx,%rcx,8)
movq 0x20(%rsp), %rdx
movl %ebx, (%rdx,%rcx,4)
movl $0x18, %esi
movq %rax, %rdi
callq 0x2180
movl $0xc, %esi
movq %r15, %rdi
callq 0x2180
leaq 0x78(%rsp), %rdi
callq 0x64a2
leaq 0x38(%rsp), %rdi
callq 0x64a2
movl %ebx, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0xc, %esi
movq %r15, %rdi
callq 0x2180
jmp 0x3bc9
movq %rax, %rbx
jmp 0x3bc9
jmp 0x3ba7
jmp 0x3ba7
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq %r15, %rdi
je 0x3bc9
movq (%rdi), %r14
movl $0x18, %esi
callq 0x2180
movq %r14, %rdi
cmpq %r15, %r14
jne 0x3bb4
leaq 0x78(%rsp), %rdi
callq 0x64a2
leaq 0x38(%rsp), %rdi
callq 0x64a2
movq %rbx, %rdi
callq 0x2270