// File: /workspace/github_cpp_makefile_star2_O2/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 $0xb8, %rsp
movq %r9, 0x48(%rsp)
movq %r8, 0x50(%rsp)
leaq 0xa0(%rsp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
andq $0x0, 0x10(%rax)
movl %edx, %ebp
movq %rsi, 0x10(%rsp)
leaq 0x88(%rsp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
andq $0x0, 0x10(%rax)
movq %rdi, %rbx
andq $0x0, 0x20(%rsp)
andq $0x0, 0x18(%rsp)
xorl %r13d, %r13d
leaq 0x30(%rsp), %r14
leaq 0x7c0c(%rip), %r12      # 0xb18c
leaq 0x58(%rsp), %r15
cmpq $0x3, %r13
je 0x361d
movq %r14, 0x38(%rsp)
movq %r14, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
movsbl (%r13,%r12), %edx
movq %r15, %rdi
movq %rbx, %rsi
movl %ebp, %ecx
callq 0x347a
movq %r14, %rdi
movq %r15, %rsi
callq 0x60d8
movq %r15, %rdi
callq 0x5f18
leaq 0xa0(%rsp), %rdi
movq %r14, %rsi
callq 0x5c20
movsbl (%r13,%r12), %edx
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movl %ebp, %ecx
callq 0x347a
movq %r14, %rdi
movq %r15, %rsi
callq 0x60d8
movq %r15, %rdi
callq 0x5f18
leaq 0x88(%rsp), %rdi
movq %r14, %rsi
callq 0x5c20
movq %r14, %rdi
callq 0x5f18
incq %r13
jmp 0x3585
leaq 0x58(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x30(%rsp), %rdi
pushq $0x3
popq %rsi
leaq 0x80(%rsp), %rcx
callq 0x5c2c
leaq 0x80(%rsp), %rdx
andq $0x0, (%rdx)
leaq 0x58(%rsp), %rdi
pushq $0x3
popq %rsi
leaq 0xf(%rsp), %rcx
callq 0x5c82
xorl %r15d, %r15d
xorpd %xmm1, %xmm1
xorl %ebx, %ebx
cmpq $0x3, %r15
je 0x37f8
movsd %xmm1, 0x70(%rsp)
movl %ebx, 0x2c(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x20(%rsp), %rdi
movl %r15d, %esi
callq 0x5cd8
movq 0x88(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0x18(%rsp), %rdi
movl %r15d, %esi
callq 0x5cd8
movq 0x20(%rsp), %r10
addq $0x10, %r10
movq 0x18(%rsp), %rbp
addq $0x10, %rbp
movq 0x30(%rsp), %rax
movq %rax, 0x78(%rsp)
movq %r10, 0x10(%rsp)
movq (%r10), %r10
cmpq 0x10(%rsp), %r10
je 0x37ae
movl 0x10(%r10), %r11d
movl 0x14(%r10), %ecx
movl %ecx, %ebx
subl %r11d, %ebx
movl %ebx, %eax
cltd
pushq $0x2
popq %rsi
idivl %esi
movl %eax, %esi
cvtsi2sd %ebx, %xmm0
movq 0x58(%rsp), %r12
movb $0x1, %al
movq 0x30(%rsp), %r13
movq %rbp, %r8
movq (%r8), %r8
cmpq %rbp, %r8
je 0x3798
movl 0x10(%r8), %edi
movl 0x14(%r8), %r9d
cmpl %ecx, %r9d
movl %ecx, %r14d
cmovll %r9d, %r14d
cmpl %edi, %r11d
movl %edi, %edx
cmovgl %r11d, %edx
subl %edx, %r14d
jle 0x3702
movl %r9d, %eax
subl %edi, %eax
cmpl %r9d, %ecx
cmovgl %ecx, %r9d
cmpl %r11d, %edi
cmovgel %r11d, %edi
subl %edi, %r9d
movl %r9d, %edx
subl %r14d, %edx
cmpl %edx, %r14d
cmovll %r14d, %edx
cvtsi2sd %edx, %xmm1
cltd
pushq $0x2
popq %rdi
idivl %edi
cmpl %esi, %eax
cmovgel %esi, %eax
cvtsi2sd %eax, %xmm2
minsd %xmm1, %xmm2
cvttsd2si %xmm2, %eax
addl %r14d, %eax
cvtsi2sd %eax, %xmm1
cvtsi2sd %r9d, %xmm2
divsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
addsd (%r12,%r15,8), %xmm1
movsd %xmm1, (%r12,%r15,8)
addl %ebx, (%r13,%r15,4)
xorl %eax, %eax
jmp 0x3702
testb $0x1, %al
je 0x36ca
movq 0x78(%rsp), %rax
addl %ebx, (%rax,%r15,4)
jmp 0x36ca
movq 0x58(%rsp), %rax
movsd (%rax,%r15,8), %xmm0
movq 0x30(%rsp), %rcx
movl (%rcx,%r15,4), %ecx
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
movq 0x48(%rsp), %rdx
movsd %xmm0, (%rdx,%r15,8)
movsd 0x70(%rsp), %xmm1
addsd (%rax,%r15,8), %xmm1
movl 0x2c(%rsp), %ebx
addl %ecx, %ebx
movq 0x50(%rsp), %rax
movl %ecx, (%rax,%r15,4)
incq %r15
jmp 0x3661
cvtsi2sd %ebx, %xmm0
divsd %xmm0, %xmm1
movslq 0xf0(%rsp), %rax
movq 0x48(%rsp), %rcx
movsd %xmm1, (%rcx,%rax,8)
movq 0x50(%rsp), %rcx
movl %ebx, (%rcx,%rax,4)
leaq 0x58(%rsp), %rdi
callq 0x63c8
leaq 0x30(%rsp), %rdi
callq 0x62e0
leaq 0x88(%rsp), %rdi
callq 0x5f78
leaq 0xa0(%rsp), %rdi
callq 0x5f78
movl %ebx, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x3877
movq %rax, %rbx
jmp 0x3890
jmp 0x3883
jmp 0x3883
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x63c8
leaq 0x30(%rsp), %rdi
callq 0x62e0
jmp 0x3890
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x5f18
leaq 0x88(%rsp), %rdi
callq 0x5f78
leaq 0xa0(%rsp), %rdi
callq 0x5f78
movq %rbx, %rdi
callq 0x22c0