// File: nanjiangshu[P]calSOV/calSOV.cpp
// Function: ReadNextSeq_FASTA(_IO_FILE*, char*, int*, int, char*, int)
int ReadNextSeq_FASTA(FILE *fp, char* seq, int *pSeq_type /*= NULL*/, int maxlength /*= LONGEST_SEQ*/, char* annotationLine/*=NULL*/, int maxSizeAnnotationLine /*=50*/)/*{{{*/
    /****************************************************************************
     * ReadNextSeq_FASTA()
     * read in the fasta format sequence from the file stream 
     * return the length of the sequence if successful
     * return 0 or minus value if no more sequence can be read from the file stream
     * The leading white spaces are ignored
     * check the type (DNA or AA) of fasta sequence file based the annotation line 
     * Last modified, 2007-02-12, Nanjiang Shu
     * Updated 2010-04-22: the annotation line (without the leading ">") can be
     * read in, note that the maxSizeAnnotationLine must be supplied
     ***************************************************************************/
{
    int   c;
    int   i;

    do{  /* ignore the leading white spaces */ 
        c = getc(fp);
    }while(isspace(c));

    if(pSeq_type != NULL) 
        *pSeq_type = UNKNOWN_SEQ_TYPE; /* initializing sequence type*/

    if(c  == '>') 
    { 
        Array1D <char> line_1darray(maxSizeAnnotationLine +1);
        char *line = line_1darray.array1D;
        fgetline(fp,line,maxSizeAnnotationLine);
        if( pSeq_type != NULL)
        {
            if(strstr(line, "protein") != NULL)
                *pSeq_type = AA_SEQ;
            else if( strstr(line,"nucleic") != NULL)
                *pSeq_type = DNA_SEQ;
            else if( strstr(line,"shape") != NULL)
                *pSeq_type = SHAPE_SEQ;
            else
                *pSeq_type = UNKNOWN_SEQ_TYPE;
        }
        if (annotationLine != NULL)
        {
            my_strcpy(annotationLine,line, maxSizeAnnotationLine-1);  /*read in the annotation line*/ 
        }
    }
    else  
    {
        fseek(fp, -1, SEEK_CUR); /* backward 1 byte of file stream if there is no annotation line*/ 
    }

    i = 0 ;
    while((c = getc(fp)) != EOF) 
    {
        if(c == '>') 
        {
            fseek(fp, -1, SEEK_CUR); /* backward 1 byte of file stream*/
            break;  /* read in the first sequence if there are multiple sequences in the file*/ 
        }
        if(! isspace(c)) /* neglect white spaces and return characters in sequence region*/ 
        {
            seq[i] = c ; i ++ ;
            if(i >= maxlength)
            {
                fprintf(stderr,"Error, sequence longer then maxlength = %d\n", maxlength);
                return 1;

            }
        }
    }
    seq[i] = '\0' ;
    if(c == EOF && i == 0)
        return EOF;
    else
        return i; /* return the length of sequence*/ 
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movq -0x10(%rbp), %rdi
callq 0x20d0
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %edi
callq 0x2060
cmpl $0x0, %eax
jne 0x2bbf
cmpq $0x0, -0x20(%rbp)
je 0x2be9
movq -0x20(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
cmpl $0x3e, -0x38(%rbp)
jne 0x2cdb
movl -0x34(%rbp), %esi
incl %esi
leaq -0x50(%rbp), %rdi
callq 0x8320
movq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
movl -0x34(%rbp), %edx
callq 0x2dc0
jmp 0x2c1b
cmpq $0x0, -0x20(%rbp)
je 0x2cb6
movq -0x58(%rbp), %rdi
leaq 0xab44(%rip), %rsi      # 0xd775
callq 0x20b0
cmpq $0x0, %rax
je 0x2c62
movq -0x20(%rbp), %rax
movl $0x1, (%rax)
jmp 0x2cb4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x8360
jmp 0x2dae
movq -0x58(%rbp), %rdi
leaq 0xab10(%rip), %rsi      # 0xd77d
callq 0x20b0
cmpq $0x0, %rax
je 0x2c84
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
jmp 0x2cb2
movq -0x58(%rbp), %rdi
leaq 0xaaf6(%rip), %rsi      # 0xd785
callq 0x20b0
cmpq $0x0, %rax
je 0x2ca6
movq -0x20(%rbp), %rax
movl $0x2, (%rax)
jmp 0x2cb0
movq -0x20(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
jmp 0x2cb2
jmp 0x2cb4
jmp 0x2cb6
cmpq $0x0, -0x30(%rbp)
je 0x2cd0
movq -0x30(%rbp), %rdi
movq -0x58(%rbp), %rsi
movl -0x34(%rbp), %edx
subl $0x1, %edx
callq 0x2830
leaq -0x50(%rbp), %rdi
callq 0x8360
jmp 0x2cf0
movq -0x10(%rbp), %rdi
movq $-0x1, %rsi
movl $0x1, %edx
callq 0x22b0
movl $0x0, -0x3c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x20d0
movl %eax, -0x38(%rbp)
cmpl $-0x1, %eax
je 0x2d7e
cmpl $0x3e, -0x38(%rbp)
jne 0x2d25
movq -0x10(%rbp), %rdi
movq $-0x1, %rsi
movl $0x1, %edx
callq 0x22b0
jmp 0x2d7e
movl -0x38(%rbp), %edi
callq 0x2060
cmpl $0x0, %eax
jne 0x2d79
movl -0x38(%rbp), %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jl 0x2d77
movq 0x1027e(%rip), %rax     # 0x12fd8
movq (%rax), %rdi
movl -0x24(%rbp), %edx
leaq 0xaa24(%rip), %rsi      # 0xd78b
movb $0x0, %al
callq 0x2330
movl $0x1, -0x4(%rbp)
jmp 0x2da5
jmp 0x2d79
jmp 0x2cf7
movq -0x18(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movb $0x0, (%rax,%rcx)
cmpl $-0x1, -0x38(%rbp)
jne 0x2d9f
cmpl $0x0, -0x3c(%rbp)
jne 0x2d9f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2da5
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x2380
nopw (%rax,%rax)