// File: /workspace/github_c_makefile_star2_O2/yhg926[P]public_kssd/command_dist.c
// Function: run_stageI
const char * run_stageI (dist_opt_val_t *opt_val, infile_tab_t *seqfile_stat,
      int* shuffled_seqfname_ind, const char *co_dir, int p_fit_mem)
{
 llong **CO = malloc( p_fit_mem * sizeof(llong *) );
  for(int i = 0; i< p_fit_mem; i++ ){
   CO[i] = (llong *)malloc(hashsize * sizeof(llong) );
 }
 llong all_ctx_ct = 0 ;
 ctx_obj_ct_t *ctx_ct_list = malloc(sizeof(ctx_obj_ct_t) * seqfile_stat->infile_num);
 if(opt_val->byread){
  for(int i = 0; i< seqfile_stat->infile_num; i++){
   char* seqfname = seqfile_stat->organized_infile_tab[ shuffled_seqfname_ind[i] ].fpath;
   printf("decomposing %s by reads\n",seqfname) ;
   reads2mco(seqfname, co_dir, opt_val->pipecmd);
  }
 }
 else {
  int num_threads = seqfile_stat->infile_num > p_fit_mem ? p_fit_mem : 1;
  int fcount = 0;
#pragma omp parallel for num_threads(num_threads) reduction(+:all_ctx_ct) schedule(guided)
   for(int i = 0; i< seqfile_stat->infile_num; i++){
     int tid = 0;
#ifdef _OPENMP
      tid = omp_get_thread_num();
#endif
      char* seqfname = seqfile_stat->organized_infile_tab[ shuffled_seqfname_ind[i] ].fpath;
      char cofname[PATHLEN];
      sprintf(cofname,"%s/%d.co",co_dir,i);
      llong *co;
      if(isOK_fmt_infile(seqfname,fastq_fmt,FQ_FMT_SZ) || opt_val->pipecmd[0]!='\0'){
    if(opt_val->abundance){
     co = mt_shortreads2koc(seqfname,CO[tid],opt_val->pipecmd, p_fit_mem);
      ctx_ct_list[i] = write_fqkoc2files(cofname,co);
    }
    else{
     co = fastq2co(seqfname,CO[tid],opt_val->pipecmd,opt_val->kmerqlty,opt_val->kmerocrs);
         ctx_ct_list[i] = write_fqco2file(cofname,co);
    }
       }
       else{
    if(opt_val->abundance) {
     opt_val->abundance = 0;
     printf("Warning: close abundance mode (-A) since non-fastq file input.\n");
    }
    if(opt_val->u)
     co = uniq_fasta2co(seqfname,CO[tid],opt_val->pipecmd);
    else
      co = fasta2co(seqfname,CO[tid],opt_val->pipecmd);
        ctx_ct_list[i] = wrt_co2cmpn_use_inn_subctx(cofname,co);
       }
   all_ctx_ct += ctx_ct_list[i] ;
#pragma omp atomic
   fcount++;
   printf("%d/%d decomposing %s\r",fcount,seqfile_stat->infile_num,seqfname) ;
  }
  printf("\n");
#pragma omp parallel for num_threads(p_fit_mem) schedule(guided)
    for(int c = 0; c < component_num; c++){
   size_t *cof_index_in_cbdco = malloc( (seqfile_stat->infile_num + 1) * sizeof(size_t) );
      char tmpfname[PATHLEN];
      FILE *com_cofp,*indexfp, *com_abund_fp;
     sprintf(tmpfname,"%s/%s.%d",co_dir,skch_prefix,c);
   if( (com_cofp = fopen(tmpfname,"wb")) == NULL) err(errno,"%s",tmpfname);
      sprintf(tmpfname,"%s/%s.%d",co_dir,idx_prefix,c);
      if( (indexfp = fopen(tmpfname,"wb")) == NULL) err(errno,"%s",tmpfname);
   if(opt_val->abundance) {
    sprintf(tmpfname,"%s/%s.%d.a",co_dir,skch_prefix,c);
    com_abund_fp = fopen(tmpfname,"wb");
    if( com_abund_fp == NULL) err(errno,"%s",tmpfname);
   }
      void *tmp_mem = malloc( LD_FCTR * 2 * (1LLU << (4*COMPONENT_SZ - CTX_SPC_USE_L)) *sizeof(unsigned int) );
      struct stat tmpstat;
      FILE *tmpfp;
      cof_index_in_cbdco[0] = 0;
      for(int i = 0; i< seqfile_stat->infile_num; i++){
       sprintf(tmpfname,"%s/%d.co.%d", co_dir, i, c);
        if( (tmpfp = fopen(tmpfname,"rb")) == NULL) err(errno,"%s",tmpfname);
        stat(tmpfname,&tmpstat);
        int tmpkmerct = tmpstat.st_size/sizeof(unsigned int);
        cof_index_in_cbdco[i+1] = (size_t)cof_index_in_cbdco[i] + tmpkmerct;
        fread(tmp_mem,tmpstat.st_size,1,tmpfp);
        fwrite(tmp_mem,tmpstat.st_size,1,com_cofp);
        fclose(tmpfp);
        remove(tmpfname);
    if(opt_val->abundance) {
     sprintf(tmpfname,"%s/%d.co.%d.a", co_dir, i, c);
     if( (tmpfp = fopen(tmpfname,"rb")) == NULL) err(errno,"%s",tmpfname);
     fread(tmp_mem, sizeof(unsigned short)*tmpkmerct, 1 , tmpfp);
     fwrite(tmp_mem, sizeof(unsigned short)*tmpkmerct, 1 , com_abund_fp );
     fclose(tmpfp);
     remove(tmpfname);
    }
      }
      fclose(com_cofp);
   if(opt_val->abundance) fclose(com_abund_fp) ;
   free(tmp_mem);
      fwrite(cof_index_in_cbdco,sizeof(size_t), seqfile_stat->infile_num + 1, indexfp);
      fclose(indexfp);
      free(cof_index_in_cbdco);
    }
 }
  for(int i = 0; i< p_fit_mem; i++ ) free(CO[i]);
  free(CO);
  co_dstat_t co_dstat_wrout;
 co_dstat_wrout.shuf_id = dim_shuffle->dim_shuffle_stat.id ;
 co_dstat_wrout.koc = opt_val->abundance ;
 co_dstat_wrout.kmerlen = dim_shuffle->dim_shuffle_stat.k * 2;
 co_dstat_wrout.dim_rd_len = dim_shuffle->dim_shuffle_stat.drlevel * 2 ;
  co_dstat_wrout.comp_num = component_num ;
  co_dstat_wrout.infile_num = seqfile_stat->infile_num;
 co_dstat_wrout.all_ctx_ct = all_ctx_ct;
  char *co_dstat_fullname = malloc(PATHLEN*sizeof(char) );
  sprintf(co_dstat_fullname, "%s/%s",co_dir,co_dstat);
  FILE *coutfp;
  if ( ( coutfp = fopen(co_dstat_fullname,"wb")) == NULL ) err(errno,"%s",co_dstat_fullname);
   fwrite(&co_dstat_wrout,sizeof(co_dstat_wrout),1,coutfp);
 fwrite(ctx_ct_list,sizeof(ctx_obj_ct_t),co_dstat_wrout.infile_num,coutfp);
 free(ctx_ct_list);
 for(int i = 0; i< co_dstat_wrout.infile_num; i++)
  fwrite(seqfile_stat->organized_infile_tab[ shuffled_seqfname_ind[i] ].fpath,PATHLEN,1,coutfp);
  fclose(coutfp);
 return (const char *)co_dstat_fullname;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movl %r8d, %ebp
movq %rcx, %r12
movq %rdx, 0x38(%rsp)
movq %rsi, (%rsp)
movq %rdi, 0x10(%rsp)
movslq %r8d, %r15
leaq (,%r15,8), %rdi
callq 0x3340
leaq 0x56a77(%rip), %rcx     # 0x5d628
movl (%rcx), %ebx
shlq $0x3, %rbx
xorl %r14d, %r14d
testl %r15d, %r15d
movq %rax, %r15
movl $0x0, %r13d
movl %ebp, 0x18(%rsp)
cmovgl %ebp, %r13d
cmpq %r14, %r13
je 0x6be4
movq %rbx, %rdi
callq 0x3340
movq %rax, (%r15,%r14,8)
incq %r14
jmp 0x6bce
movq %r13, 0x68(%rsp)
movq %r15, 0x28(%rsp)
movq %r12, 0x8(%rsp)
movq (%rsp), %rax
movslq (%rax), %r15
movq %r15, %rdi
shlq $0x2, %rdi
callq 0x3340
movq %rax, 0x48(%rsp)
movq 0x10(%rsp), %rbx
leaq 0x44b(%rbx), %r14
cmpb $0x1, 0x64c(%rbx)
jne 0x6c7f
movq $0x0, 0x30(%rsp)
leaq 0xe0e6(%rip), %rbx      # 0x14d16
xorl %r12d, %r12d
movq (%rsp), %r13
movslq %r15d, %rax
cmpq %rax, %r12
jge 0x6dc6
movq 0x8(%r13), %rax
movq 0x38(%rsp), %rcx
movslq (%rcx,%r12,4), %rcx
shlq $0x4, %rcx
movq 0x8(%rax,%rcx), %r15
movq %rbx, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x3170
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x1182f
incq %r12
movl (%r13), %r15d
jmp 0x6c37
leaq 0x70(%rsp), %r13
leaq 0xe0ad(%rip), %r12      # 0x14d38
xorl %ebp, %ebp
movq $0x0, 0x30(%rsp)
movslq %r15d, %rax
cmpq %rax, %rbp
jge 0x6f2a
movq (%rsp), %rax
movq 0x8(%rax), %rax
movq 0x38(%rsp), %rcx
movslq (%rcx,%rbp,4), %rcx
shlq $0x4, %rcx
movq 0x8(%rax,%rcx), %r15
movq %r13, %rdi
leaq 0xe069(%rip), %rsi      # 0x14d2f
movq 0x8(%rsp), %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3430
movq %r15, %rdi
leaq 0x16302(%rip), %rsi     # 0x1cfe0
pushq $0x2
popq %rdx
callq 0x87ff
testl %eax, %eax
jne 0x6cf0
cmpb $0x0, (%r14)
je 0x6d42
movq 0x28(%rsp), %rax
movq (%rax), %rsi
cmpb $0x1, 0x449(%rbx)
jne 0x6d1d
movq %r15, %rdi
movq %r14, %rdx
movl 0x18(%rsp), %ecx
callq 0x12d6d
movq %r13, %rdi
movq %rax, %rsi
callq 0x127a4
jmp 0x6d92
movl 0x424(%rbx), %r8d
movl 0x428(%rbx), %ecx
movq %r15, %rdi
movq %r14, %rdx
callq 0x120c4
movq %r13, %rdi
movq %rax, %rsi
callq 0x12ac0
jmp 0x6d92
cmpb $0x1, 0x449(%rbx)
jne 0x6d5e
movb $0x0, 0x449(%rbx)
leaq 0xe808(%rip), %rdi      # 0x15561
callq 0x30a0
movq 0x28(%rsp), %rax
movq (%rax), %rsi
cmpb $0x1, 0x44a(%rbx)
jne 0x6d7c
movq %r15, %rdi
movq %r14, %rdx
callq 0x13129
jmp 0x6d87
movq %r15, %rdi
movq %r14, %rdx
callq 0x11d7c
movq %r13, %rdi
movq %rax, %rsi
callq 0x12c1b
movq 0x48(%rsp), %rcx
movl %eax, (%rcx,%rbp,4)
movl %eax, %eax
addq %rax, 0x30(%rsp)
incq %rbp
movq (%rsp), %rbx
movl (%rbx), %edx
movq %r12, %rdi
movl %ebp, %esi
movq %r15, %rcx
xorl %eax, %eax
callq 0x3170
movl (%rbx), %r15d
movq 0x10(%rsp), %rbx
jmp 0x6c96
movq 0x8(%rsp), %r13
movq 0x28(%rsp), %r14
xorl %ebx, %ebx
cmpq %rbx, 0x68(%rsp)
je 0x6de7
movq (%r14,%rbx,8), %rdi
callq 0x3040
incq %rbx
jmp 0x6dd2
movq %r14, %rdi
callq 0x3040
leaq 0x5681a(%rip), %rax     # 0x5d610
movq (%rax), %rax
movl (%rax), %ecx
movl %ecx, 0x70(%rsp)
movq 0x10(%rsp), %rcx
movb 0x449(%rcx), %cl
movb %cl, 0x74(%rsp)
movl 0x4(%rax), %ecx
addl %ecx, %ecx
movl %ecx, 0x78(%rsp)
movl 0xc(%rax), %eax
addl %eax, %eax
movl %eax, 0x7c(%rsp)
leaq 0x56809(%rip), %rax     # 0x5d630
movl (%rax), %eax
movl %eax, 0x80(%rsp)
movq (%rsp), %rax
movl (%rax), %eax
movl %eax, 0x84(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x88(%rsp)
movl $0x100, %edi            # imm = 0x100
callq 0x3340
movq %rax, %rbx
leaq 0xd535(%rip), %rsi      # 0x14393
leaq 0xd8fb(%rip), %rcx      # 0x14760
movq %r13, %rdx
xorl %r13d, %r13d
movq %rax, %rdi
xorl %eax, %eax
callq 0x3430
leaq 0xd1ee(%rip), %rsi      # 0x1406a
movq %rbx, %rdi
callq 0x33a0
testq %rax, %rax
je 0x7216
movq %rax, %r14
leaq 0x70(%rsp), %r12
pushq $0x20
popq %rsi
pushq $0x1
popq %r15
movq %r12, %rdi
movq %r15, %rdx
movq %rax, %rcx
callq 0x3450
movslq 0x14(%r12), %r12
pushq $0x4
popq %rsi
movq 0x48(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rdx
movq %r14, %rcx
callq 0x3450
movq %rbp, %rdi
callq 0x3040
testl %r12d, %r12d
movl $0x0, %ebp
cmovgl %r12d, %ebp
cmpq %r13, %rbp
je 0x6f0d
movq (%rsp), %rax
movq 0x8(%rax), %rax
movq 0x38(%rsp), %rcx
movslq (%rcx,%r13,4), %rcx
shlq $0x4, %rcx
movq 0x8(%rax,%rcx), %rdi
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdx
movq %r14, %rcx
callq 0x3450
incq %r13
jmp 0x6ed9
movq %r14, %rdi
callq 0x3100
movq %rbx, %rax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0xa
popq %rdi
callq 0x3050
leaq 0xd125(%rip), %r15      # 0x1405e
leaq 0x70(%rsp), %r12
xorl %ebp, %ebp
movq 0x8(%rsp), %r13
leaq 0x566e4(%rip), %rax     # 0x5d630
cmpl (%rax), %ebp
movq 0x28(%rsp), %r14
jge 0x6dd0
movq (%rsp), %rax
movslq (%rax), %rax
leaq 0x8(,%rax,8), %rdi
callq 0x3340
movq %rax, 0x40(%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
leaq 0xeffa(%rip), %rcx      # 0x15f7c
movl %ebp, %r8d
xorl %eax, %eax
callq 0x3430
movq %r12, %rdi
leaq 0xd0d4(%rip), %r14      # 0x1406a
movq %r14, %rsi
callq 0x33a0
movq %rax, 0x58(%rsp)
testq %rax, %rax
je 0x71fc
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
leaq 0xefc7(%rip), %rcx      # 0x15f83
movl %ebp, %r8d
xorl %eax, %eax
callq 0x3430
movq %r12, %rdi
movq %r14, %rsi
callq 0x33a0
movq %rax, 0x60(%rsp)
testq %rax, %rax
je 0x71fc
cmpb $0x1, 0x449(%rbx)
jne 0x701f
movq %r12, %rdi
leaq 0xd4d2(%rip), %rsi      # 0x144c4
movq %r13, %rdx
leaq 0xef80(%rip), %rcx      # 0x15f7c
movl %ebp, %r8d
xorl %eax, %eax
callq 0x3430
movq %r12, %rdi
movq %r14, %rsi
callq 0x33a0
movq %rax, 0x50(%rsp)
testq %rax, %rax
je 0x71fc
movl $0x4ccccc, %edi         # imm = 0x4CCCCC
callq 0x3340
movq %rax, 0x18(%rsp)
movq 0x40(%rsp), %rax
andq $0x0, (%rax)
xorl %ebx, %ebx
movl %ebp, 0x24(%rsp)
movq (%rsp), %rax
movslq (%rax), %rax
cmpq %rax, %rbx
jge 0x7190
movq %r12, %rdi
leaq 0xdcf7(%rip), %rsi      # 0x14d4e
movq %r13, %rdx
movl %ebx, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x3430
movq %r12, %rdi
leaq 0xcff7(%rip), %rsi      # 0x14067
callq 0x33a0
testq %rax, %rax
je 0x71fc
movq %rax, %r14
movq %r12, %rdi
leaq 0x178(%rsp), %rsi
callq 0x32e0
movq 0x1a8(%rsp), %r13
movq %r13, %rax
shrq $0x2, %rax
movslq %eax, %rbp
movq 0x40(%rsp), %rcx
movq (%rcx,%rbx,8), %rax
addq %rbp, %rax
movq %rax, 0x8(%rcx,%rbx,8)
movq 0x18(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
pushq $0x1
popq %r12
movq %r12, %rdx
movq %r14, %rcx
callq 0x30c0
movq %r15, %rdi
movq %r13, %rsi
movq 0x8(%rsp), %r13
movq %r12, %rdx
leaq 0x70(%rsp), %r12
movq 0x58(%rsp), %rcx
callq 0x3450
movq %r14, %rdi
callq 0x3100
movq %r12, %rdi
callq 0x3070
movq 0x10(%rsp), %rax
cmpb $0x1, 0x449(%rax)
jne 0x7184
movq %r12, %rdi
leaq 0xdc47(%rip), %rsi      # 0x14d5a
movq %r13, %rdx
movl %ebx, %ecx
movl 0x24(%rsp), %r8d
xorl %eax, %eax
callq 0x3430
movq %r12, %rdi
leaq 0xcf39(%rip), %rsi      # 0x14067
callq 0x33a0
testq %rax, %rax
je 0x71fc
movq %rax, %r14
addq %rbp, %rbp
movq 0x18(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
pushq $0x1
popq %r12
movq %r12, %rdx
movq %rax, %rcx
callq 0x30c0
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
leaq 0x70(%rsp), %r12
movq 0x50(%rsp), %rcx
callq 0x3450
movq %r14, %rdi
callq 0x3100
movq %r12, %rdi
callq 0x3070
incq %rbx
movl 0x24(%rsp), %ebp
jmp 0x703d
movq 0x58(%rsp), %rdi
callq 0x3100
movq 0x10(%rsp), %rbx
cmpb $0x0, 0x449(%rbx)
je 0x71b2
movq 0x50(%rsp), %rdi
callq 0x3100
movq 0x18(%rsp), %rdi
callq 0x3040
movq (%rsp), %rax
movslq (%rax), %rdx
incq %rdx
movq 0x40(%rsp), %r14
movq %r14, %rdi
pushq $0x8
popq %rsi
movq 0x60(%rsp), %r15
movq %r15, %rcx
callq 0x3450
movq %r15, %rdi
callq 0x3100
movq %r14, %rdi
callq 0x3040
incl %ebp
leaq 0xce67(%rip), %r15      # 0x1405e
jmp 0x6f45
callq 0x3060
movl (%rax), %edi
leaq 0xe16c(%rip), %rsi      # 0x15376
leaq 0x70(%rsp), %rdx
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0xe152(%rip), %rsi      # 0x15376
movq %rbx, %rdx
jmp 0x720f