// File: /workspace/github_cpp_makefile_star2_O3/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, 0x20(%rsp)
movq %r8, 0x28(%rsp)
movl %edx, %ebp
movq %rsi, 0x8(%rsp)
movq %rdi, 0x10(%rsp)
leaq 0x40(%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 0x7945(%rip), %rax      # 0xb164
movsbl (%r13,%rax), %edx
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movl %ebp, %ecx
callq 0x36db
movq %r15, %rdi
movq %r12, %rsi
callq 0x669e
movq 0x90(%rsp), %rdi
cmpq %r12, %rdi
je 0x3861
movq (%rdi), %rbx
movl $0x18, %esi
callq 0x2180
movq %rbx, %rdi
cmpq %r12, %rbx
jne 0x384c
leaq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0x6736
movq %rax, %rdi
leaq 0x40(%rsp), %rsi
callq 0x2050
incq 0x50(%rsp)
leaq 0x78dd(%rip), %rax      # 0xb164
movsbl (%r13,%rax), %edx
movq %r12, %rdi
movq 0x8(%rsp), %rsi
movl %ebp, %ecx
callq 0x36db
movq %r15, %rdi
movq %r12, %rsi
callq 0x669e
movq 0x90(%rsp), %rdi
cmpq %r12, %rdi
je 0x38c9
movq (%rdi), %rbx
movl $0x18, %esi
callq 0x2180
movq %rbx, %rdi
cmpq %r12, %rbx
jne 0x38b4
movq %r14, %rdi
movq %r15, %rsi
callq 0x6736
movq %rax, %rdi
movq %r14, %rsi
callq 0x2050
incq 0x88(%rsp)
movq 0x60(%rsp), %rdi
cmpq %r15, %rdi
je 0x3906
movq (%rdi), %rbx
movl $0x18, %esi
callq 0x2180
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x38f1
incq %r13
cmpq $0x3, %r13
jne 0x3805
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 0x40(%rsp), %rcx
movq %rcx, 0x38(%rsp)
xorpd %xmm0, %xmm0
xorl %edx, %edx
movq 0x78(%rsp), %rcx
movq %rcx, 0x30(%rsp)
xorl %edi, %edi
xorl %ebx, %ebx
movl %ebx, 0x1c(%rsp)
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %r9
testq %rdi, %rdi
je 0x3999
movq %rdx, %rcx
movq 0x38(%rsp), %rsi
movq (%rsi), %rsi
incq %rcx
jne 0x3981
movq %rdx, %rcx
movq 0x30(%rsp), %r9
movq (%r9), %r9
incq %rcx
jne 0x3991
movq %rdx, 0x58(%rsp)
movq 0x10(%rsi), %r10
addq $0x10, %rsi
cmpq %rsi, %r10
je 0x3aa7
movq 0x10(%r9), %r11
addq $0x10, %r9
movq %rsi, 0x10(%rsp)
movq %r11, 0x8(%rsp)
movl 0x10(%r10), %r14d
movl 0x14(%r10), %ebx
movl %ebx, %ebp
subl %r14d, %ebp
cmpq %r9, %r11
je 0x3a97
movl %ebp, %r13d
shrl $0x1f, %r13d
addl %ebp, %r13d
sarl %r13d
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
xorl %edx, %edx
movl 0x10(%r11), %r8d
movl 0x14(%r11), %esi
cmpl %ebx, %esi
movl %ebx, %r12d
cmovll %esi, %r12d
cmpl %r8d, %r14d
movl %r8d, %ecx
cmovgl %r14d, %ecx
subl %ecx, %r12d
jle 0x3a7c
movl %esi, %ecx
subl %r8d, %ecx
cmpl %esi, %ebx
cmovgl %ebx, %esi
cmpl %r14d, %r8d
cmovgel %r14d, %r8d
subl %r8d, %esi
movl %esi, %edx
subl %r12d, %edx
cmpl %edx, %r12d
cmovll %r12d, %edx
xorps %xmm2, %xmm2
cvtsi2sd %edx, %xmm2
movl %ecx, %edx
shrl $0x1f, %edx
addl %ecx, %edx
sarl %edx
cmpl %r13d, %edx
cmovgel %r13d, %edx
xorps %xmm3, %xmm3
cvtsi2sd %edx, %xmm3
minsd %xmm2, %xmm3
cvttsd2si %xmm3, %ecx
addl %r12d, %ecx
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %esi, %xmm3
divsd %xmm3, %xmm2
mulsd %xmm1, %xmm2
addsd (%rax,%rdi,8), %xmm2
movsd %xmm2, (%rax,%rdi,8)
addl %ebp, (%r15,%rdi,4)
movb $0x1, %dl
movq (%r11), %r11
cmpq %r9, %r11
jne 0x39ed
testb $0x1, %dl
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %r11
jne 0x3a9b
addl %ebp, (%r15,%rdi,4)
movq (%r10), %r10
cmpq %rsi, %r10
jne 0x39c1
movsd (%rax,%rdi,8), %xmm1
movl (%r15,%rdi,4), %ecx
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
addsd %xmm1, %xmm0
divsd %xmm2, %xmm1
movq 0x20(%rsp), %rdx
movsd %xmm1, (%rdx,%rdi,8)
movl 0x1c(%rsp), %ebx
addl %ecx, %ebx
movq 0x28(%rsp), %rdx
movl %ecx, (%rdx,%rdi,4)
incq %rdi
movq 0x58(%rsp), %rdx
decq %rdx
cmpq $0x3, %rdi
jne 0x3966
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
divsd %xmm1, %xmm0
movslq 0xe0(%rsp), %rcx
movq 0x20(%rsp), %rdx
movsd %xmm0, (%rdx,%rcx,8)
movq 0x28(%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 0x65ce
leaq 0x40(%rsp), %rdi
callq 0x65ce
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 0x3b90
movq %rax, %rbx
jmp 0x3b90
jmp 0x3b6e
jmp 0x3b6e
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq %r15, %rdi
je 0x3b90
movq (%rdi), %r14
movl $0x18, %esi
callq 0x2180
movq %r14, %rdi
cmpq %r15, %r14
jne 0x3b7b
leaq 0x78(%rsp), %rdi
callq 0x65ce
leaq 0x40(%rsp), %rdi
callq 0x65ce
movq %rbx, %rdi
callq 0x2270