// File: 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
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x84b0
leaq -0x60(%rbp), %rdi
callq 0x84b0
leaq -0x68(%rbp), %rdi
callq 0x84d0
leaq -0x70(%rbp), %rdi
callq 0x84d0
leaq -0x78(%rbp), %rdi
callq 0x84f0
leaq -0x80(%rbp), %rdi
callq 0x84f0
movl $0x0, -0x84(%rbp)
movl $0x0, -0x84(%rbp)
cmpl $0x3, -0x84(%rbp)
jge 0x4257
leaq -0xa0(%rbp), %rdi
callq 0x83b0
movq -0x8(%rbp), %rsi
movslq -0x84(%rbp), %rax
leaq 0xf09a(%rip), %rcx      # 0x131f4
movsbl (%rax,%rcx), %edx
movl -0x14(%rbp), %ecx
leaq -0xb8(%rbp), %rdi
callq 0x3f70
jmp 0x416f
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x1c0(%rbp)
leaq -0xb8(%rbp), %rsi
movq %rsi, -0x1c8(%rbp)
callq 0x8510
movq -0x1c8(%rbp), %rdi
callq 0x8490
movq -0x1c0(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x8540
jmp 0x41ae
movq -0x10(%rbp), %rsi
movslq -0x84(%rbp), %rax
leaq 0xf034(%rip), %rcx      # 0x131f4
movsbl (%rax,%rcx), %edx
movl -0x14(%rbp), %ecx
leaq -0xe0(%rbp), %rdi
callq 0x3f70
jmp 0x41d5
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x1d0(%rbp)
leaq -0xe0(%rbp), %rsi
movq %rsi, -0x1d8(%rbp)
callq 0x8510
movq -0x1d8(%rbp), %rdi
callq 0x8490
movq -0x1d0(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x8540
jmp 0x4214
leaq -0xa0(%rbp), %rdi
callq 0x8490
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x412f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x8490
jmp 0x48bf
movl $0x0, -0xfc(%rbp)
leaq -0xfd(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
callq 0x8580
movq -0x1e0(%rbp), %rcx
leaq -0xf8(%rbp), %rdi
movl $0x3, %esi
leaq -0xfc(%rbp), %rdx
callq 0x85a0
jmp 0x4295
leaq -0xfd(%rbp), %rdi
callq 0x8620
movq $0x0, -0x120(%rbp)
leaq -0x121(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0x8640
movq -0x1e8(%rbp), %rcx
leaq -0x118(%rbp), %rdi
movl $0x3, %esi
leaq -0x120(%rbp), %rdx
callq 0x8660
jmp 0x42e0
leaq -0x121(%rbp), %rdi
callq 0x86e0
movl $0x0, -0x128(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x130(%rbp)
movl $0x0, -0x84(%rbp)
cmpl $0x3, -0x84(%rbp)
jge 0x4837
leaq -0x48(%rbp), %rdi
callq 0x8700
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rax
movq %rax, -0x68(%rbp)
movl -0x84(%rbp), %esi
leaq -0x68(%rbp), %rdi
callq 0x8730
jmp 0x4344
leaq -0x60(%rbp), %rdi
callq 0x8700
movq %rax, -0x170(%rbp)
movq -0x170(%rbp), %rax
movq %rax, -0x70(%rbp)
movl -0x84(%rbp), %esi
leaq -0x70(%rbp), %rdi
callq 0x8730
jmp 0x4370
leaq -0x68(%rbp), %rdi
callq 0x8780
movq %rax, %rdi
callq 0x87a0
movq %rax, -0x178(%rbp)
movq -0x178(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x8780
movq %rax, %rdi
callq 0x8800
movq %rax, -0x180(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x180(%rbp), %rsi
callq 0x87d0
testb $0x1, %al
jne 0x43c4
jmp 0x4766
leaq -0x78(%rbp), %rdi
callq 0x8830
movl (%rax), %eax
movl %eax, -0x134(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x8830
movl 0x4(%rax), %eax
movl %eax, -0x138(%rbp)
movl -0x138(%rbp), %eax
subl -0x134(%rbp), %eax
movl %eax, -0x150(%rbp)
movb $0x0, -0x181(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x8780
movq %rax, %rdi
callq 0x87a0
movq %rax, -0x190(%rbp)
movq -0x190(%rbp), %rax
movq %rax, -0x80(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x8780
movq %rax, %rdi
callq 0x8800
movq %rax, -0x198(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x198(%rbp), %rsi
callq 0x87d0
testb $0x1, %al
jne 0x4454
jmp 0x471d
leaq -0x80(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x8830
movq -0x1f8(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x13c(%rbp)
callq 0x8830
movl 0x4(%rax), %eax
movl %eax, -0x140(%rbp)
movl -0x140(%rbp), %eax
movl -0x13c(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x154(%rbp)
movl -0x134(%rbp), %edi
movl -0x138(%rbp), %esi
movl -0x13c(%rbp), %edx
movl -0x140(%rbp), %ecx
callq 0x27e0
movl %eax, -0x1ec(%rbp)
jmp 0x44ba
movl -0x1ec(%rbp), %eax
movl %eax, -0x144(%rbp)
cmpl $0x0, -0x144(%rbp)
jle 0x459a
leaq -0x138(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x7de0
movq %rax, -0x200(%rbp)
jmp 0x44ef
movq -0x200(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x20c(%rbp)
leaq -0x134(%rbp), %rdi
leaq -0x13c(%rbp), %rsi
callq 0x7da0
movq %rax, -0x208(%rbp)
jmp 0x451a
movq -0x208(%rbp), %rcx
movl -0x20c(%rbp), %eax
subl (%rcx), %eax
movl %eax, -0x148(%rbp)
jmp 0x45a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
leaq -0xfd(%rbp), %rdi
callq 0x8620
jmp 0x48bf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
leaq -0x121(%rbp), %rdi
callq 0x86e0
jmp 0x48b3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x88f0
jmp 0x48b3
movl $0x0, -0x148(%rbp)
cmpl $0x0, -0x144(%rbp)
jle 0x470d
movb $0x1, -0x181(%rbp)
movl -0x148(%rbp), %eax
movl -0x144(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x1a4(%rbp)
leaq -0x1a4(%rbp), %rdi
leaq -0x144(%rbp), %rsi
callq 0x7da0
movq %rax, -0x218(%rbp)
jmp 0x45e8
movq -0x218(%rbp), %rax
cvtsi2sdl (%rax), %xmm0
movsd %xmm0, -0x1a0(%rbp)
movl -0x150(%rbp), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
sarl %eax
movl %eax, -0x1b4(%rbp)
movl -0x154(%rbp), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
sarl %eax
movl %eax, -0x1b8(%rbp)
leaq -0x1b4(%rbp), %rdi
leaq -0x1b8(%rbp), %rsi
callq 0x7da0
movq %rax, -0x220(%rbp)
jmp 0x4641
movq -0x220(%rbp), %rax
cvtsi2sdl (%rax), %xmm0
movsd %xmm0, -0x1b0(%rbp)
leaq -0x1a0(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x8850
movq %rax, -0x228(%rbp)
jmp 0x4670
movq -0x228(%rbp), %rax
cvttsd2si (%rax), %eax
movl %eax, -0x14c(%rbp)
movl -0x144(%rbp), %eax
addl -0x14c(%rbp), %eax
cvtsi2sd %eax, %xmm0
cvtsi2sdl -0x148(%rbp), %xmm1
divsd %xmm1, %xmm0
cvtsi2sdl -0x150(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x160(%rbp)
movsd -0x160(%rbp), %xmm0
movsd %xmm0, -0x238(%rbp)
movslq -0x84(%rbp), %rsi
leaq -0x118(%rbp), %rdi
callq 0x8890
movsd -0x238(%rbp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movl -0x150(%rbp), %eax
movl %eax, -0x22c(%rbp)
movslq -0x84(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x88b0
movl -0x22c(%rbp), %ecx
addl (%rax), %ecx
movl %ecx, (%rax)
jmp 0x470f
leaq -0x80(%rbp), %rdi
callq 0x88d0
jmp 0x4423
movb -0x181(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x4756
movl -0x150(%rbp), %eax
movl %eax, -0x23c(%rbp)
movslq -0x84(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x88b0
movl -0x23c(%rbp), %ecx
addl (%rax), %ecx
movl %ecx, (%rax)
jmp 0x4758
leaq -0x78(%rbp), %rdi
callq 0x88d0
jmp 0x4393
movslq -0x84(%rbp), %rsi
leaq -0x118(%rbp), %rdi
callq 0x8890
movsd (%rax), %xmm0
movsd %xmm0, -0x248(%rbp)
movslq -0x84(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x88b0
movsd -0x248(%rbp), %xmm0
cvtsi2sdl (%rax), %xmm1
divsd %xmm1, %xmm0
movq -0x30(%rbp), %rax
movslq -0x84(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movslq -0x84(%rbp), %rsi
leaq -0x118(%rbp), %rdi
callq 0x8890
movsd (%rax), %xmm0
addsd -0x130(%rbp), %xmm0
movsd %xmm0, -0x130(%rbp)
movslq -0x84(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x88b0
movl (%rax), %eax
addl -0x128(%rbp), %eax
movl %eax, -0x128(%rbp)
movslq -0x84(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x88b0
movl (%rax), %edx
movq -0x28(%rbp), %rax
movslq -0x84(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x430b
movsd -0x130(%rbp), %xmm0
cvtsi2sdl -0x128(%rbp), %xmm1
divsd %xmm1, %xmm0
movq -0x30(%rbp), %rax
movslq 0x10(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x128(%rbp), %edx
movq -0x28(%rbp), %rax
movslq 0x10(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x28(%rbp), %rax
movslq 0x10(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x24c(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x88f0
leaq -0xf8(%rbp), %rdi
callq 0x8950
leaq -0x60(%rbp), %rdi
callq 0x89b0
leaq -0x48(%rbp), %rdi
callq 0x89b0
movl -0x24c(%rbp), %eax
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
leaq -0xf8(%rbp), %rdi
callq 0x8950
leaq -0x60(%rbp), %rdi
callq 0x89b0
leaq -0x48(%rbp), %rdi
callq 0x89b0
movq -0xc0(%rbp), %rdi
callq 0x2380
nopl (%rax)