// File: /workspace/github_c_makefile_star2_O3/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 0x4b74a(%rip), %eax     # 0x5e618
subl 0x4b750(%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 0x4b73c(%rip), %r14     # 0x5e630
leaq (,%r14,8), %rbx
movq %rbx, %rdi
callq 0x3340
movq %rax, 0x28(%rsp)
movq %rbx, %rdi
callq 0x3340
movq %rax, 0x48(%rsp)
movq %rbx, %rdi
callq 0x3340
movq %rax, 0x40(%rsp)
testq %r14, %r14
jle 0x12fd9
leaq 0x160(%rsp), %rbx
leaq 0x2143(%rip), %r15      # 0x1507e
leaq 0x260(%rsp), %r12
leaq 0x2120(%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 0x2123(%rip), %rsi      # 0x15091
movq %r13, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3430
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3430
movq %rbx, %rdi
movq %r14, %rsi
callq 0x33a0
movq 0x40(%rsp), %rcx
movq %rax, (%rcx,%rbp,8)
testq %rax, %rax
je 0x1339e
movq %r12, %rdi
movq %r14, %rsi
callq 0x33a0
movq 0x48(%rsp), %rcx
movq %rax, (%rcx,%rbp,8)
testq %rax, %rax
je 0x133b6
incq %rbp
movslq 0x4b660(%rip), %rax     # 0x5e630
cmpq %rax, %rbp
jl 0x12f4c
movq 0x8(%rsp), %rdx
cmpb $0x0, (%rdx)
je 0x1300f
leaq 0x516a(%rip), %rsi      # 0x18154
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x3430
leaq 0x3e09(%rip), %rsi      # 0x16e0e
movq %rbx, %rdi
callq 0x3390
jmp 0x13020
leaq 0x3df8(%rip), %rsi      # 0x16e0e
movq 0x10(%rsp), %rdi
callq 0x33a0
movq %rax, 0x20(%rsp)
testq %rax, %rax
movq 0x28(%rsp), %r15
je 0x13412
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 0x133fd
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 0x130a9
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 0x132fa
movq %rbp, 0x8(%rsp)
movq %r14, %rbp
movslq %r13d, %rax
movsbq 0x360(%rsp,%rax), %r14
leaq 0x4a3c(%rip), %rax      # 0x17b00
movslq (%rax,%r14,4), %rax
cmpq $-0x1, %rax
je 0x131d7
movq %rbp, %r14
shlq $0x2, %r14
orq %rax, %r14
andq 0x4b555(%rip), %r14     # 0x5e638
movq 0x8(%rsp), %rdx
shrq $0x2, %rdx
xorq $0x3, %rax
movb 0x4b53e(%rip), %cl      # 0x5e634
shlq %cl, %rax
movq %rax, %rbp
addq %rdx, %rbp
movq 0x38(%rsp), %rcx
incq %rcx
movslq 0x4b532(%rip), %rax     # 0x5e640
movq %rcx, 0x38(%rsp)
cmpq %rax, %rcx
jbe 0x131ee
cmpq %rbp, %r14
movq %rbp, %rdx
cmovbq %r14, %rdx
movq 0x4b51b(%rip), %rsi     # 0x5e648
andq %rdx, %rsi
movl 0x4b4e9(%rip), %r8d     # 0x5e620
leal (%r8,%r8), %ecx
shrq %cl, %rsi
movq 0x4b513(%rip), %rdi     # 0x5e658
movslq %esi, %rsi
movslq (%rdi,%rsi,4), %rdi
cmpl 0x4b50e(%rip), %edi     # 0x5e660
jae 0x131ee
movq 0x4b4f1(%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 0x4b49b(%rip), %cl      # 0x5e624
shlb $0x2, %cl
shrq %cl, %rsi
addq %rdi, %rsi
movslq 0x4b497(%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 0x40(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
movl $0x4, %esi
movl $0x1, %edx
leaq 0x30(%rsp), %rdi
callq 0x3450
jmp 0x131ee
cmpb $0xa, %r14b
je 0x131e6
movzbl %r14b, %eax
cmpl $0xd, %eax
jne 0x13200
movq %rbp, %r14
movq 0x8(%rsp), %rbp
leal 0x1(%r13), %eax
cmpl %ebx, %r13d
jl 0x13078
jmp 0x132fa
callq 0x34d0
movl $0x1, %ecx
movq %rcx, 0x38(%rsp)
cmpb $0x3e, %r14b
jne 0x131e6
movq (%rax), %rax
movzwl (%rax,%r14,2), %eax
andl $0x400, %eax            # imm = 0x400
jne 0x131e6
cmpq 0x18(%rsp), %r12
jbe 0x13291
cmpl $0x0, 0x4b3fe(%rip)     # 0x5e630
jle 0x13288
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 0x133d3
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 0x4b3ad(%rip), %rax     # 0x5e630
cmpq %rax, %r14
jl 0x13249
addq $0xf4240, 0x18(%rsp)    # imm = 0xF4240
movq %rbp, %r14
incq %r12
cmpl %ebx, %r13d
movq 0x8(%rsp), %rbp
jge 0x131ee
movslq %r13d, %rax
cmpb $0xa, 0x360(%rsp,%rax)
je 0x131ee
leal -0x1(%rbx), %eax
cmpl %eax, %r13d
jge 0x132c3
incl %r13d
jmp 0x132f0
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 0x133e6
cmpl %ebx, %r13d
jl 0x132a5
jmp 0x131ee
cmpl $0x0, 0x4b32f(%rip)     # 0x5e630
jle 0x13374
xorl %r15d, %r15d
leaq 0x30(%rsp), %rbx
movq $0x0, 0x30(%rsp)
movq 0x28(%rsp), %rax
movq (%rax,%r15,8), %r13
movq 0x48(%rsp), %rax
movq (%rax,%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 0x13328
movq 0x40(%rsp), %rax
movq (%rax,%r15,8), %rdi
callq 0x3100
movq %r14, %rdi
callq 0x3100
incq %r15
movslq 0x4b2c1(%rip), %rax     # 0x5e630
cmpq %rax, %r15
jl 0x1330b
leaq 0x4e6b(%rip), %rdi      # 0x181e6
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 0x301a(%rip), %rsi      # 0x163c6
leaq 0x160(%rsp), %rdx
jmp 0x133cc
callq 0x3060
movl (%rax), %edi
leaq 0x3002(%rip), %rsi      # 0x163c6
leaq 0x260(%rsp), %rdx
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x4daf(%rip), %rsi      # 0x18190
movl %r14d, %edx
jmp 0x133f6
callq 0x3060
movl (%rax), %edi
leaq 0x4db9(%rip), %rsi      # 0x181ad
movl %ebx, %edx
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x4d5e(%rip), %rsi      # 0x18169
movq 0x10(%rsp), %rdx
jmp 0x133cc
callq 0x3060
movl (%rax), %edi
leaq 0x4d3a(%rip), %rsi      # 0x1815a
leaq 0x60(%rsp), %rdx
jmp 0x133cc