// File: /workspace/github_cpp_makefile_star2_O2/nanjiangshu[P]calSOV/calSOV.cpp
// Function: CalSOV_obsolete(char*, char*, int, double*, int*, double*, int)
int CalSOV_obsolete(char *obsSec, char *predSec, int seqLength, double *numCorrectHSR, int *numHSR, double *sovHSR, int numState = 3)/*{{{*/
    /*=======================================================================
     * numHSR[4] Helix, Sheet, Coil, All
     * sovHSR[4]
     * numState = 3 by default
     *====================================================================*/
{
    int Min_overlap[500] ;
    int Max_extend[500]  ;
    int LenSeg_pred[500];
    int All_HSRLen[3];
    double All_Sum[3];

    int iterHSRState = 0; /*iterator for the HSR state, H, S and R*/
    char thisHSRstate = ' '; /*current HSR state*/
    int i = 0;
    int j = 0;

    for (i=0;i<3;i++)
    {
        All_HSRLen[i] = 0;
        All_Sum[i] = 0.0; 
    }

    int Length = seqLength +1 ;  /*Here Length is 1 residue longer than seqLength*/
    for (iterHSRState=0; iterHSRState<3; iterHSRState++)//corrsponding to H, S, R
    {
        //for helix
        int HSRLen = 0;/*iterator for secondary structure elements*/
        thisHSRstate = HSRalphabet[iterHSRState];
        for(i=0; i<Length; i++)
        { 
            if (  obsSec[i] == thisHSRstate  )
            {
                HSRLen++;
            }
            else
            { 
                if (  HSRLen >= 1  )
                { 
                    int Num_Segment = 0;//the number of segments in an observed element
                    int SubLen = 0;  /*length of the sub segment*/
                    //Min_overlap[]--the length of overlapping
                    //Mam_extend[] -- the extended length of two segments to be compared
                    for (j=i-HSRLen; j<i; j++)
                    { 
                        if (  predSec[j] == thisHSRstate  )
                        { 
                            SubLen++;
                        } 
                        else
                        { 
                            if (  SubLen >= 1  )
                            {
                                Min_overlap[Num_Segment] = SubLen;
                                Num_Segment++;
                            } 
                            SubLen = 0;
                        } 
                    }
                    if (  SubLen >= 1  )
                    {
                        Min_overlap[Num_Segment] = SubLen;
                        Num_Segment++;
                    }
                    double sum = 0;
                    int beg = 0;
                    int Nextend = 0;
                    int Num_HSRLen = HSRLen;
                    if  (  Num_Segment >= 1  )
                    {
                        for (j=0; j<Num_Segment; j++)
                        {
                            Max_extend[j] = HSRLen;
                            LenSeg_pred[j] = Min_overlap[j];
                        }
                        //for first segment
                        if (  predSec[i-HSRLen] == thisHSRstate  )
                        { 
                            beg = i - HSRLen - 1;
                            Nextend = 0;
                            while (   (beg>=0) && (predSec[beg]==thisHSRstate) && (obsSec[beg]!=thisHSRstate)   )
                            {
                                Nextend++;
                                beg--;
                            }
                            Max_extend[0] = Max_extend[0] + Nextend;
                            LenSeg_pred[0] = LenSeg_pred[0] + Nextend;
                        }
                        //for last segment
                        if (  predSec[i-1] == thisHSRstate  )
                        { 
                            beg = i;
                            Nextend = 0;
                            while (   (beg<Length) && (predSec[beg]==thisHSRstate) && (obsSec[beg]!=thisHSRstate)   )
                            {
                                Nextend++;
                                beg++;
                            }
                            Max_extend[Num_Segment-1] = Max_extend[Num_Segment-1] + Nextend;
                            LenSeg_pred[Num_Segment-1] = LenSeg_pred[Num_Segment-1] + Nextend;
                        }
                        for (j=0; j<Num_Segment; j++)
                        {
                            int Nmin1 = min( (Max_extend[j]-Min_overlap[j]), Min_overlap[j]);
                            int Nmin2 = min( HSRLen/2, LenSeg_pred[j]/2);
                            int Nmin = min(Nmin1, Nmin2);
                            double score_seg =  (Min_overlap[j]+Nmin)*1.0/double(Max_extend[j]) ;
                            sum = sum + score_seg;
                        }
                        sum = sum*HSRLen;
                        Num_HSRLen = HSRLen*Num_Segment;
                    }

                    All_HSRLen[iterHSRState] = All_HSRLen[iterHSRState] + Num_HSRLen;
                    All_Sum[iterHSRState] = All_Sum[iterHSRState] + sum;
                } 
                HSRLen = 0;
            }  
        } 
    }

    double sumOverlap = 0;
    int sumAllRes = 0;
    for (i=0; i<numState; i++)
    {
        numHSR[i] = All_HSRLen[i];
        numCorrectHSR[i] = All_Sum[i];
        sovHSR[i] = All_Sum[i]/double(All_HSRLen[i]+1e-6);
        sumAllRes  += All_HSRLen[i];
        sumOverlap += All_Sum[i];
    }
    numHSR[numState] = sumAllRes;
    numCorrectHSR[numState] = sumOverlap;
    sovHSR[numState] = sumOverlap / double(sumAllRes+1e-6);

    return numHSR[numState];
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1768, %rsp           # imm = 0x1768
movq %r9, -0x60(%rsp)
movq %r8, -0x78(%rsp)
movq %rcx, -0x70(%rsp)
movq %rdi, %r13
xorl %eax, %eax
cmpq $0x3, %rax
je 0x38ed
andl $0x0, -0x3c(%rsp,%rax,4)
andq $0x0, -0x30(%rsp,%rax,8)
incq %rax
jmp 0x38d7
movslq %edx, %r11
xorl %r14d, %r14d
movq %r11, -0x50(%rsp)
movq %r13, -0x58(%rsp)
cmpq $0x3, %r14
je 0x3af2
leaq 0x787e(%rip), %rax      # 0xb18c
movb (%r14,%rax), %r15b
xorl %r12d, %r12d
movq %rsi, %rdi
movq %r13, %rax
xorl %ecx, %ecx
movq %r14, -0x48(%rsp)
cmpq %r11, %r12
jg 0x3aea
cmpb %r15b, (%r13,%r12)
jne 0x3939
incl %ecx
jmp 0x3adc
testl %ecx, %ecx
jle 0x39f4
movq %rax, -0x68(%rsp)
movl %r12d, %eax
subl %ecx, %eax
movslq %eax, %rdx
xorl %ebx, %ebx
xorl %r8d, %r8d
movslq %ebx, %r9
cmpq %r12, %rdx
jge 0x3987
cmpb %r15b, (%rsi,%rdx)
jne 0x3969
incl %r8d
movl %r8d, %r10d
jmp 0x397f
xorl %r10d, %r10d
testl %r8d, %r8d
jle 0x397f
movl %r8d, 0xf90(%rsp,%r9,4)
incl %r9d
movl %r9d, %ebx
incq %rdx
movl %r10d, %r8d
jmp 0x3953
testl %r8d, %r8d
jle 0x399a
movl %r8d, 0xf90(%rsp,%r9,4)
incl %r9d
movl %r9d, %ebx
testl %ebx, %ebx
jle 0x39fb
movl %ecx, %r8d
movq %r12, %rdx
subq %r8, %rdx
movl %ebx, %r9d
xorl %r8d, %r8d
cmpq %r8, %r9
je 0x39cc
movl %ecx, 0x7c0(%rsp,%r8,4)
movl 0xf90(%rsp,%r8,4), %r10d
movl %r10d, -0x10(%rsp,%r8,4)
incq %r8
jmp 0x39ad
cmpb %r15b, (%rsi,%rdx)
movq -0x68(%rsp), %r8
jne 0x3a0f
movl %eax, %edx
xorl %eax, %eax
testl %edx, %edx
jle 0x3a04
cmpb %r15b, -0x1(%rsi,%rdx)
jne 0x3a04
cmpb %r15b, -0x1(%r13,%rdx)
je 0x3a04
decl %eax
decq %rdx
jmp 0x39db
xorl %ecx, %ecx
jmp 0x3adc
xorpd %xmm0, %xmm0
jmp 0x3ac2
subl %eax, 0x7c0(%rsp)
subl %eax, -0x10(%rsp)
cmpb %r15b, -0x1(%rsi,%r12)
jne 0x3a40
xorl %eax, %eax
leaq (%r12,%rax), %rdx
cmpq %r11, %rdx
jg 0x3a32
cmpb %r15b, (%rdi,%rax)
jne 0x3a32
cmpb %r15b, (%r8,%rax)
je 0x3a32
incq %rax
jmp 0x3a18
leal -0x1(%rbx), %edx
addl %eax, 0x7c0(%rsp,%rdx,4)
addl %eax, -0x10(%rsp,%rdx,4)
movl %ecx, %r14d
shrl %r14d
xorpd %xmm0, %xmm0
xorl %r8d, %r8d
cmpq %r8, %r9
je 0x3aa6
movl 0x7c0(%rsp,%r8,4), %ebp
movl 0xf90(%rsp,%r8,4), %r11d
movl %ebp, %r10d
subl %r11d, %r10d
cmpl %r10d, %r11d
cmovll %r11d, %r10d
movl -0x10(%rsp,%r8,4), %eax
cltd
movl $0x2, %r13d
idivl %r13d
cmpl %r14d, %eax
jl 0x3a86
movl %r14d, %eax
cmpl %r10d, %eax
jl 0x3a8e
movl %r10d, %eax
addl %r11d, %eax
cvtsi2sd %eax, %xmm1
cvtsi2sd %ebp, %xmm2
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
incq %r8
jmp 0x3a4d
cvtsi2sd %ecx, %xmm1
mulsd %xmm1, %xmm0
imull %ecx, %ebx
movl %ebx, %ecx
movq -0x58(%rsp), %r13
movq -0x50(%rsp), %r11
movq -0x48(%rsp), %r14
addl %ecx, -0x3c(%rsp,%r14,4)
addsd -0x30(%rsp,%r14,8), %xmm0
movsd %xmm0, -0x30(%rsp,%r14,8)
xorl %ecx, %ecx
movq -0x68(%rsp), %rax
incq %r12
incq %rax
incq %rdi
jmp 0x3922
incq %r14
jmp 0x38fd
xorl %ecx, %ecx
movl 0x17a0(%rsp), %eax
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
xorpd %xmm0, %xmm0
movsd 0x34f7(%rip), %xmm1     # 0x7008
xorl %eax, %eax
movq -0x60(%rsp), %rdi
cmpq %rcx, %rdx
je 0x3b57
movl -0x3c(%rsp,%rcx,4), %esi
movq -0x78(%rsp), %r8
movl %esi, (%r8,%rcx,4)
movsd -0x30(%rsp,%rcx,8), %xmm2
movq -0x70(%rsp), %r8
movsd %xmm2, (%r8,%rcx,8)
cvtsi2sd %esi, %xmm3
addsd %xmm1, %xmm3
addsd %xmm2, %xmm0
divsd %xmm3, %xmm2
movsd %xmm2, (%rdi,%rcx,8)
addl %esi, %eax
incq %rcx
jmp 0x3b18
movslq 0x17a0(%rsp), %rcx
movq -0x78(%rsp), %rdx
movl %eax, (%rdx,%rcx,4)
cvtsi2sd %eax, %xmm1
addsd 0x3495(%rip), %xmm1     # 0x7008
movq -0x70(%rsp), %rdx
movsd %xmm0, (%rdx,%rcx,8)
divsd %xmm1, %xmm0
movsd %xmm0, (%rdi,%rcx,8)
addq $0x1768, %rsp           # imm = 0x1768
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq