// File: /workspace/github_c_makefile_star2_O2/yhg926[P]public_kssd/iseq2comem.c
// Function: reads2mco
int reads2mco(char* seqfname,const char *co_dir, char * pipecmd){
#define unit_incrs 1000000
int comp_code_bits = half_ctx_len - drlevel > COMPONENT_SZ ? 4*(half_ctx_len - drlevel - COMPONENT_SZ ) : 0 ;
 size_t **cof_count = malloc( component_num * sizeof (size_t*) );
 FILE **outindf = malloc(component_num * sizeof(FILE *));
 FILE **outf = malloc(component_num * sizeof(FILE *));
 char indexfname[PATHLEN]; char combined_cof[PATHLEN];
 size_t cof_count_sz = unit_incrs;
 for(int i=0;i<component_num ;i++)
  {
  cof_count[i] = (size_t *)calloc( cof_count_sz , sizeof(size_t) );
  sprintf(combined_cof,"%s/combco.%d",co_dir,i);
  sprintf(indexfname,"%s/combco.index.%d",co_dir,i);
   if( (outf[i] = fopen(combined_cof,"wb")) == NULL) err(errno,"%s",combined_cof);
    if( (outindf[i] = fopen(indexfname,"wb")) == NULL) err(errno,"%s",indexfname);
  };
 FILE *infp;
 char fas_fname[PATHLEN];
 if(pipecmd[0] != '\0'){
  sprintf(fas_fname,"%s %s",pipecmd,seqfname);
  if( (infp=popen(fas_fname,"r")) == NULL ) err(errno,"reads2mco():%s",fas_fname);
 }
 else
  if( (infp=fopen(seqfname,"r")) == NULL ) err(errno,"reads2mco():%s",fas_fname);;
 char seqin_buff[ READSEQ_BUFFSZ + 1 ];
 int newLen = fread(seqin_buff, sizeof(char),READSEQ_BUFFSZ,infp);
 if(! (newLen >0) ) err(errno,"reads2mco():eof or fread error file=%s",seqfname);
 llong base = 1; char ch; int basenum;
 llong tuple = 0LLU, crvstuple = 0LLU,
  unituple, drtuple, pfilter;
 llong readn = 0;
 for(int pos = 0; pos <= newLen; pos++)
  {
    if(pos == newLen){
        newLen = fread(seqin_buff, sizeof(char),READSEQ_BUFFSZ,infp);
      if(newLen > 0)
        pos = 0;
      else break;
    };
    ch = seqin_buff[pos];
    basenum = Basemap[(int)ch];
    if(basenum != DEFAULT)
    {
      tuple = ( ( tuple<< 2 ) | (llong)basenum ) & tupmask ;
      crvstuple = ( crvstuple >> 2 ) + (((llong)basenum^3LLU) << crvsaddmove);
      base++;
    }
    else if ( (ch == '\n') || (ch == '\r') ) { continue;}
    else if (isalpha(ch)){ base=1; continue; }
    else if ( ch == '>' )
    {
   if( readn > cof_count_sz ){
    for(int i=0;i<component_num ;i++){
     size_t *newtmp = (size_t *)realloc(cof_count[i], (cof_count_sz + unit_incrs) * sizeof(size_t));
     if (newtmp != NULL) {
       cof_count[i] = newtmp;
      memset(cof_count[i] + cof_count_sz ,0, unit_incrs * sizeof(size_t) );
     }
     else err(errno,"cof_count[%d] realloc failed",i);
    }
    cof_count_sz += unit_incrs;
   }
   readn++;
     while( (pos < newLen ) && ( seqin_buff[pos] != '\n' ) )
      {
        if (pos < newLen - 1)
          pos++;
        else
        {
          newLen = fread(seqin_buff, sizeof(char),READSEQ_BUFFSZ,infp);
          if(newLen > 0) pos = -1;
          else err(errno,"fasta2co(): can not find seqences head start from '>' %d",newLen);
        };
      };
      base = 1;
      continue;
    }
    else {
      base=1;
      continue;
    };
    if( base > TL )
    {
      unituple = tuple < crvstuple ? tuple:crvstuple;
      int dim_tup = ((unituple & domask) >> ( (half_outctx_len)*2 ) ) ;
      pfilter = dim_shuf_arr[dim_tup];
      if( ( pfilter >= dim_end) || (pfilter < dim_start ) ) continue;
      pfilter = pfilter - dim_start;
      drtuple = ( ( (unituple & undomask)
              + ( ( unituple & ( ( 1LLU<< ( half_outctx_len*2) ) - 1)) << (TL*2 - half_outctx_len*4) ) )
              >> ( drlevel*4 ) )
              + pfilter ;
   cof_count[drtuple % component_num][readn]++;
   unsigned int newid = (unsigned int)( drtuple >> comp_code_bits ) ;
   fwrite( &newid, sizeof(unsigned int),1,outf[(int)( drtuple % component_num )] );
   };
  };
 for(int i=0;i<component_num ;i++){
  llong cumval = 0 ;
  for(llong n=0; n<=readn;n++ ){
   cumval += cof_count[i][n];
   fwrite(&cumval, sizeof(llong),1,outindf[i]);
  }
  fclose(outf[i]);
  fclose(outindf[i]);
 }
 printf("decomposing %s by reads is complete!\n",seqfname);
 return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10358, %rsp          # imm = 0x10358
movq %rdx, 0x8(%rsp)
movq %rsi, %r13
movl 0x4bdca(%rip), %eax     # 0x5d618
subl 0x4bdd0(%rip), %eax     # 0x5d624
movq %rdi, 0x10(%rsp)
xorl %ecx, %ecx
cmpl $0x8, %eax
leal -0x1c(,%rax,4), %eax
cmovll %ecx, %eax
movq %rax, 0x48(%rsp)
movslq 0x4bdbc(%rip), %rbx     # 0x5d630
shlq $0x3, %rbx
movq %rbx, %rdi
callq 0x3340
movq %rax, 0x28(%rsp)
movq %rbx, %rdi
callq 0x3340
movq %rax, 0x40(%rsp)
movq %rbx, %rdi
callq 0x3340
movq %rax, 0x38(%rsp)
leaq 0x150(%rsp), %r15
leaq 0x27d0(%rip), %r12      # 0x1407e
leaq 0x250(%rsp), %r14
leaq 0x27ad(%rip), %rbp      # 0x1406a
xorl %ebx, %ebx
movslq 0x4bd6a(%rip), %rax     # 0x5d630
cmpq %rax, %rbx
jge 0x11965
movl $0xf4240, %edi          # imm = 0xF4240
pushq $0x8
popq %rsi
callq 0x32a0
movq 0x28(%rsp), %rcx
movq %rax, (%rcx,%rbx,8)
movq %r15, %rdi
leaq 0x27a2(%rip), %rsi      # 0x14091
movq %r13, %rdx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x3430
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x3430
movq %r15, %rdi
movq %rbp, %rsi
callq 0x33a0
movq 0x38(%rsp), %rcx
movq %rax, (%rcx,%rbx,8)
testq %rax, %rax
je 0x11d0a
movq %r14, %rdi
movq %rbp, %rsi
callq 0x33a0
movq 0x40(%rsp), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
testq %rax, %rax
jne 0x118bf
callq 0x3060
movl (%rax), %edi
leaq 0x3a1e(%rip), %rsi      # 0x15376
leaq 0x250(%rsp), %rdx
jmp 0x11d20
movq 0x8(%rsp), %rdx
cmpb $0x0, (%rdx)
je 0x1199b
leaq 0x578e(%rip), %rsi      # 0x17104
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x3430
leaq 0x442d(%rip), %rsi      # 0x15dbe
movq %rbx, %rdi
callq 0x3390
jmp 0x119ac
leaq 0x441c(%rip), %rsi      # 0x15dbe
movq 0x10(%rsp), %rdi
callq 0x33a0
movq %rax, 0x18(%rsp)
testq %rax, %rax
movq 0x28(%rsp), %rbp
je 0x11d3f
leaq 0x350(%rsp), %rdi
pushq $0x1
popq %r14
movl $0x10000, %edx          # imm = 0x10000
movq %r14, %rsi
movq 0x18(%rsp), %rcx
callq 0x30c0
movq %rax, %r13
testl %r13d, %r13d
jle 0x11d54
movl $0xf4240, %eax          # imm = 0xF4240
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
movq $0x0, 0x8(%rsp)
xorl %ebx, %ebx
cmpl %r13d, %ebx
jg 0x11c60
jne 0x11a37
movl $0x10000, %edx          # imm = 0x10000
leaq 0x350(%rsp), %rdi
pushq $0x1
popq %rsi
movq 0x18(%rsp), %rcx
callq 0x30c0
movq %rax, %r13
xorl %ebx, %ebx
testl %r13d, %r13d
jle 0x11c60
movq %r13, (%rsp)
movq %r12, %r13
movq %r15, %r12
movslq %ebx, %rax
movsbq 0x350(%rsp,%rax), %r15
leaq 0x505c(%rip), %rax      # 0x16ab0
movslq (%rax,%r15,4), %rax
cmpq $-0x1, %rax
je 0x11b59
shlq $0x2, %r13
orq %rax, %r13
andq 0x4bbc8(%rip), %r13     # 0x5d638
shrq $0x2, %r12
xorq $0x3, %rax
movb 0x4bbb6(%rip), %cl      # 0x5d634
shlq %cl, %rax
movq %rax, %r15
addq %r12, %r15
incq %r14
movslq 0x4bbaf(%rip), %rax     # 0x5d640
cmpq %rax, %r14
jbe 0x11be1
cmpq %r15, %r13
movq %r15, %rdx
cmovbq %r13, %rdx
movq 0x4bb9d(%rip), %rsi     # 0x5d648
andq %rdx, %rsi
movl 0x4bb6b(%rip), %r8d     # 0x5d620
leal (%r8,%r8), %ecx
shrq %cl, %rsi
movq 0x4bb95(%rip), %rdi     # 0x5d658
movslq %esi, %rsi
movslq (%rdi,%rsi,4), %rdi
cmpl 0x4bb90(%rip), %edi     # 0x5d660
movq %r13, %r12
jae 0x11be4
movq 0x4bb70(%rip), %r9      # 0x5d650
pushq $-0x1
popq %rsi
shlq %cl, %rsi
notq %rsi
andq %rdx, %rsi
addl %eax, %eax
shll $0x2, %r8d
subl %r8d, %eax
movl %eax, %ecx
shlq %cl, %rsi
andq %rdx, %r9
addq %r9, %rsi
movb 0x4bb1e(%rip), %cl      # 0x5d624
shlb $0x2, %cl
shrq %cl, %rsi
addq %rdi, %rsi
movslq 0x4bb1a(%rip), %rcx     # 0x5d630
movq %rsi, %rax
xorl %edx, %edx
divq %rcx
movq (%rbp,%rdx,8), %rax
movq 0x8(%rsp), %rcx
incq (%rax,%rcx,8)
movq 0x48(%rsp), %rcx
shrq %cl, %rsi
movl %esi, 0x30(%rsp)
movslq %edx, %rax
movq 0x38(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
leaq 0x30(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
callq 0x3450
jmp 0x11be4
cmpb $0xa, %r15b
je 0x11bde
movzbl %r15b, %eax
cmpl $0xd, %eax
je 0x11bde
callq 0x34d0
cmpb $0x3e, %r15b
jne 0x11bda
movq (%rax), %rax
movzwl (%rax,%r15,2), %eax
andl $0x400, %eax            # imm = 0x400
jne 0x11bda
movq 0x20(%rsp), %r14
cmpq %r14, 0x8(%rsp)
jbe 0x11bfb
leaq 0x7a1200(,%r14,8), %r15
xorl %r14d, %r14d
movslq 0x4ba90(%rip), %rax     # 0x5d630
cmpq %rax, %r14
jge 0x11bef
movq (%rbp,%r14,8), %rdi
movq %r15, %rsi
callq 0x3350
testq %rax, %rax
je 0x11d27
movq %rax, (%rbp,%r14,8)
movq 0x20(%rsp), %rcx
leaq (%rax,%rcx,8), %rdi
movl $0x7a1200, %edx         # imm = 0x7A1200
xorl %esi, %esi
callq 0x3210
incq %r14
jmp 0x11b99
pushq $0x1
popq %r14
movq %r12, %r15
movq %r13, %r12
movq (%rsp), %r13
incl %ebx
jmp 0x11a04
movq 0x20(%rsp), %r14
addq $0xf4240, %r14          # imm = 0xF4240
movq %r12, %r15
incq 0x8(%rsp)
movq %r13, %r12
movq (%rsp), %r13
leal -0x1(%r13), %eax
movslq %ebx, %rbx
cmpl %r13d, %ebx
jge 0x11c55
cmpb $0xa, 0x350(%rsp,%rbx)
je 0x11c55
cmpl %eax, %ebx
jge 0x11c29
incq %rbx
jmp 0x11c11
movl $0x10000, %edx          # imm = 0x10000
leaq 0x350(%rsp), %rdi
pushq $0x1
popq %rsi
movq 0x18(%rsp), %rcx
callq 0x30c0
movq %rax, %r13
pushq $-0x1
popq %rax
movl %eax, %ebx
testl %r13d, %r13d
jg 0x11c0a
jmp 0x11d69
movq %r14, 0x20(%rsp)
pushq $0x1
popq %r14
jmp 0x11be8
xorl %r13d, %r13d
leaq 0x30(%rsp), %rbx
pushq $0x8
popq %r14
pushq $0x1
popq %r15
movslq 0x4b9b9(%rip), %rax     # 0x5d630
cmpq %rax, %r13
jge 0x11ce2
andq $0x0, 0x30(%rsp)
movq (%rbp,%r13,8), %rbp
movq 0x40(%rsp), %rax
movq %r13, (%rsp)
movq (%rax,%r13,8), %r12
xorl %r13d, %r13d
cmpq 0x8(%rsp), %r13
ja 0x11cbe
movq (%rbp,%r13,8), %rax
addq %rax, 0x30(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x3450
incq %r13
jmp 0x11c97
movq 0x38(%rsp), %rax
movq (%rsp), %r13
movq (%rax,%r13,8), %rdi
callq 0x3100
movq %r12, %rdi
callq 0x3100
incq %r13
movq 0x28(%rsp), %rbp
jmp 0x11c70
leaq 0x54ad(%rip), %rdi      # 0x17196
movq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x3170
pushq $0x1
popq %rax
addq $0x10358, %rsp          # imm = 0x10358
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3060
movl (%rax), %edi
leaq 0x365e(%rip), %rsi      # 0x15376
leaq 0x150(%rsp), %rdx
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x540b(%rip), %rsi      # 0x17140
movl %r14d, %edx
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x53bd(%rip), %rsi      # 0x1710a
leaq 0x50(%rsp), %rdx
jmp 0x11d20
callq 0x3060
movl (%rax), %edi
leaq 0x53b7(%rip), %rsi      # 0x17119
movq 0x10(%rsp), %rdx
jmp 0x11d20
callq 0x3060
movl (%rax), %edi
leaq 0x53e6(%rip), %rsi      # 0x1715d
movl %r13d, %edx
jmp 0x11d38