// File: /workspace/github_c_makefile_star2_O1/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 $0x10368, %rsp          # imm = 0x10368
movq %rdx, 0x8(%rsp)
movq %rsi, %r13
movl 0x4c082(%rip), %eax     # 0x5e618
subl 0x4c088(%rip), %eax     # 0x5e624
movq %rdi, 0x10(%rsp)
xorl %ecx, %ecx
cmpl $0x8, %eax
leal -0x1c(,%rax,4), %eax
cmovll %ecx, %eax
movq %rax, 0x50(%rsp)
movslq 0x4c074(%rip), %rbx     # 0x5e630
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, 0x48(%rsp)
cmpl $0x0, 0x4c042(%rip)     # 0x5e630
jle 0x126a1
leaq 0x160(%rsp), %rbx
leaq 0x2a7b(%rip), %r12      # 0x1507e
leaq 0x260(%rsp), %r15
leaq 0x2a58(%rip), %r14      # 0x1506a
xorl %ebp, %ebp
movl $0xf4240, %edi          # imm = 0xF4240
movl $0x8, %esi
callq 0x32a0
movq 0x28(%rsp), %rcx
movq %rax, (%rcx,%rbp,8)
movq %rbx, %rdi
leaq 0x2a5b(%rip), %rsi      # 0x15091
movq %r13, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3430
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3430
movq %rbx, %rdi
movq %r14, %rsi
callq 0x33a0
movq 0x48(%rsp), %rcx
movq %rax, (%rcx,%rbp,8)
testq %rax, %rax
je 0x12a6c
movq %r15, %rdi
movq %r14, %rsi
callq 0x33a0
movq 0x40(%rsp), %rcx
movq %rax, (%rcx,%rbp,8)
testq %rax, %rax
je 0x12a84
incq %rbp
movslq 0x4bf98(%rip), %rax     # 0x5e630
cmpq %rax, %rbp
jl 0x12614
movq 0x8(%rsp), %rdx
cmpb $0x0, (%rdx)
je 0x126d7
leaq 0x5a72(%rip), %rsi      # 0x18124
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x3430
leaq 0x46f1(%rip), %rsi      # 0x16dbe
movq %rbx, %rdi
callq 0x3390
jmp 0x126e8
leaq 0x46e0(%rip), %rsi      # 0x16dbe
movq 0x10(%rsp), %rdi
callq 0x33a0
movq %rax, 0x20(%rsp)
testq %rax, %rax
movq 0x28(%rsp), %r15
je 0x12ae0
leaq 0x360(%rsp), %rdi
movl $0x1, %eax
movq %rax, 0x38(%rsp)
movl $0x1, %esi
movl $0x10000, %edx          # imm = 0x10000
movq 0x20(%rsp), %rcx
callq 0x30c0
movq %rax, %rbx
testl %ebx, %ebx
jle 0x12acb
xorl %eax, %eax
movl $0xf4240, %ecx          # imm = 0xF4240
movq %rcx, 0x18(%rsp)
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %r14d, %r14d
movl %eax, %r13d
cmpl %ebx, %eax
jne 0x12771
movl $0x1, %esi
movl $0x10000, %edx          # imm = 0x10000
leaq 0x360(%rsp), %rdi
movq 0x20(%rsp), %rcx
callq 0x30c0
movq %rax, %rbx
xorl %r13d, %r13d
testl %ebx, %ebx
jle 0x129c2
movq %rbp, 0x8(%rsp)
movq %r14, %rbp
movslq %r13d, %rax
movsbq 0x360(%rsp,%rax), %r14
leaq 0x5344(%rip), %rax      # 0x17ad0
movslq (%rax,%r14,4), %rax
cmpq $-0x1, %rax
je 0x1289f
movq %rbp, %r14
shlq $0x2, %r14
orq %rax, %r14
andq 0x4be8d(%rip), %r14     # 0x5e638
movq 0x8(%rsp), %rdx
shrq $0x2, %rdx
xorq $0x3, %rax
movb 0x4be76(%rip), %cl      # 0x5e634
shlq %cl, %rax
movq %rax, %rbp
addq %rdx, %rbp
movq 0x38(%rsp), %rcx
incq %rcx
movslq 0x4be6a(%rip), %rax     # 0x5e640
movq %rcx, 0x38(%rsp)
cmpq %rax, %rcx
jbe 0x128b6
cmpq %rbp, %r14
movq %rbp, %rdx
cmovbq %r14, %rdx
movq 0x4be53(%rip), %rsi     # 0x5e648
andq %rdx, %rsi
movl 0x4be21(%rip), %r8d     # 0x5e620
leal (%r8,%r8), %ecx
shrq %cl, %rsi
movq 0x4be4b(%rip), %rdi     # 0x5e658
movslq %esi, %rsi
movslq (%rdi,%rsi,4), %rdi
cmpl 0x4be46(%rip), %edi     # 0x5e660
jae 0x128b6
movq 0x4be29(%rip), %r9      # 0x5e650
movq $-0x1, %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 0x4bdd3(%rip), %cl      # 0x5e624
shlb $0x2, %cl
shrq %cl, %rsi
addq %rdi, %rsi
movslq 0x4bdcf(%rip), %rcx     # 0x5e630
movq %rsi, %rax
xorl %edx, %edx
divq %rcx
movq (%r15,%rdx,8), %rax
incq (%rax,%r12,8)
movq 0x50(%rsp), %rcx
shrq %cl, %rsi
movl %esi, 0x30(%rsp)
movslq %edx, %rax
movq 0x48(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
movl $0x4, %esi
movl $0x1, %edx
leaq 0x30(%rsp), %rdi
callq 0x3450
jmp 0x128b6
cmpb $0xa, %r14b
je 0x128ae
movzbl %r14b, %eax
cmpl $0xd, %eax
jne 0x128c8
movq %rbp, %r14
movq 0x8(%rsp), %rbp
leal 0x1(%r13), %eax
cmpl %ebx, %r13d
jl 0x12740
jmp 0x129c2
callq 0x34d0
movl $0x1, %ecx
movq %rcx, 0x38(%rsp)
cmpb $0x3e, %r14b
jne 0x128ae
movq (%rax), %rax
movzwl (%rax,%r14,2), %eax
andl $0x400, %eax            # imm = 0x400
jne 0x128ae
cmpq 0x18(%rsp), %r12
jbe 0x12959
cmpl $0x0, 0x4bd36(%rip)     # 0x5e630
jle 0x12950
movq 0x18(%rsp), %rax
leaq 0x7a1200(,%rax,8), %rax
movq %rax, 0x58(%rsp)
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
movq 0x58(%rsp), %rsi
callq 0x3350
testq %rax, %rax
je 0x12aa1
movq %rax, (%r15,%r14,8)
movq 0x18(%rsp), %rcx
leaq (%rax,%rcx,8), %rdi
movl $0x7a1200, %edx         # imm = 0x7A1200
xorl %esi, %esi
callq 0x3210
incq %r14
movslq 0x4bce5(%rip), %rax     # 0x5e630
cmpq %rax, %r14
jl 0x12911
addq $0xf4240, 0x18(%rsp)    # imm = 0xF4240
movq %rbp, %r14
incq %r12
cmpl %ebx, %r13d
movq 0x8(%rsp), %rbp
jge 0x128b6
movslq %r13d, %rax
cmpb $0xa, 0x360(%rsp,%rax)
je 0x128b6
leal -0x1(%rbx), %eax
cmpl %eax, %r13d
jge 0x1298b
incl %r13d
jmp 0x129b8
movl $0x1, %esi
movl $0x10000, %edx          # imm = 0x10000
leaq 0x360(%rsp), %rdi
movq 0x20(%rsp), %rcx
callq 0x30c0
movq %rax, %rbx
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testl %ebx, %ebx
jle 0x12ab4
cmpl %ebx, %r13d
jl 0x1296d
jmp 0x128b6
cmpl $0x0, 0x4bc67(%rip)     # 0x5e630
movq 0x40(%rsp), %r14
jle 0x12a42
xorl %r15d, %r15d
leaq 0x30(%rsp), %rbx
movq $0x0, 0x30(%rsp)
movq 0x28(%rsp), %rax
movq (%rax,%r15,8), %r13
movq (%r14,%r15,8), %r14
xorl %ebp, %ebp
movq (%r13,%rbp,8), %rax
addq %rax, 0x30(%rsp)
movl $0x8, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rcx
callq 0x3450
incq %rbp
cmpq %r12, %rbp
jbe 0x129f0
movq 0x48(%rsp), %rax
movq (%rax,%r15,8), %rdi
callq 0x3100
movq 0x40(%rsp), %r14
movq (%r14,%r15,8), %rdi
callq 0x3100
incq %r15
movslq 0x4bbf3(%rip), %rax     # 0x5e630
cmpq %rax, %r15
jl 0x129d8
leaq 0x576d(%rip), %rdi      # 0x181b6
movq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x3170
movl $0x1, %eax
addq $0x10368, %rsp          # imm = 0x10368
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3060
movl (%rax), %edi
leaq 0x38fc(%rip), %rsi      # 0x16376
leaq 0x160(%rsp), %rdx
jmp 0x12a9a
callq 0x3060
movl (%rax), %edi
leaq 0x38e4(%rip), %rsi      # 0x16376
leaq 0x260(%rsp), %rdx
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x56b1(%rip), %rsi      # 0x18160
movl %r14d, %edx
jmp 0x12ac4
callq 0x3060
movl (%rax), %edi
leaq 0x56bb(%rip), %rsi      # 0x1817d
movl %ebx, %edx
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x5660(%rip), %rsi      # 0x18139
movq 0x10(%rsp), %rdx
jmp 0x12a9a
callq 0x3060
movl (%rax), %edi
leaq 0x563c(%rip), %rsi      # 0x1812a
leaq 0x60(%rsp), %rdx
jmp 0x12a9a