// File: /workspace/github_c_makefile_star2_O2/yhg926[P]public_kssd/command_set.c
// Function: grouping_genomes
int grouping_genomes(char* taxfile){
  compan_t *subset = organize_taxf(taxfile);
  const char* co_dstat_fpath = NULL;
  co_dstat_fpath = test_get_fullpath(set_opt.insketchpath,co_dstat);
  if(co_dstat_fpath == NULL ) err(errno,"cannot find %s under %s ",co_dstat,set_opt.insketchpath);
  FILE *tmpfh;
  if( ( tmpfh = fopen(co_dstat_fpath,"rb")) == NULL ) err(errno,"grouping_genomes():%s",co_dstat_fpath);
  co_dstat_t co_dstat_readin;
  fread( &co_dstat_readin, sizeof(co_dstat_t),1,tmpfh );
  fclose(tmpfh);
  if(co_dstat_readin.infile_num != subset->gn)
    err(errno,"grouping_genomes():%s's genome number %d not matches %s's genome number %d",co_dstat_fpath,co_dstat_readin.infile_num,taxfile,subset->gn);
  free(co_dstat_fpath);
  mkdir(set_opt.outdir,0777);
  char tmppath[PATHLEN]; struct stat s; int outfn = 0; llong all_ctx_ct = 0;
  ctx_obj_ct_t *ctx_ct_list = calloc(subset->taxn,sizeof(ctx_obj_ct_t));
 unsigned int **tax_dict_ar = malloc(subset->taxn * sizeof(unsigned int *));
 int* tax_dict_size = malloc(subset->taxn*sizeof(int));
  size_t *outcombcoidx = malloc(sizeof(size_t)* (subset->taxn+1));
  for(int c=0; c < co_dstat_readin.comp_num; c++){
    sprintf(tmppath,"%s/%s.%d",set_opt.insketchpath,skch_prefix,c);
    if(stat(tmppath, &s) != 0) err(errno,"grouping_genomes():%s",tmppath);
    unsigned int *tmpcombco = malloc(s.st_size);
    if((tmpfh = fopen(tmppath,"rb")) == NULL) err(errno,"grouping_genomes():%s",tmppath);
    fread(tmpcombco,s.st_size, 1,tmpfh);
    fclose(tmpfh);
    sprintf(tmppath,"%s/%s.%d",set_opt.insketchpath,idx_prefix,c);
    if(stat(tmppath, &s) != 0) err(errno,"grouping_genomes():%s",tmppath);
    size_t *tmpcombcoidx = malloc(s.st_size);
    if((tmpfh = fopen(tmppath,"rb")) == NULL) err(errno,"grouping_genomes():%s",tmppath);
    fread(tmpcombcoidx,s.st_size, 1,tmpfh);
    fclose(tmpfh);
    for(int t = 0; t < subset->taxn; t++){
      if(subset->tax[t].taxid == 0) continue;
    int hashsize = 0;
    for(int n = 1; n <= subset->tax[t].gids[0];n++)
     hashsize += (tmpcombcoidx[subset->tax[t].gids[n]+1] - tmpcombcoidx[subset->tax[t].gids[n]]) ;
    int primer_ind = LOG2(hashsize * 1.5);
    tax_dict_size[t] = hashsize = primer_ind > 7 ? primer[primer_ind - 7] : primer[0] ;
    tax_dict_ar[t] = calloc(hashsize,sizeof(unsigned int));
        for(int n = 1; n <= subset->tax[t].gids[0];n++){
          int gid = subset->tax[t].gids[n] ;
          for(size_t i= tmpcombcoidx[gid]; i < tmpcombcoidx[gid+1];i++){
      for (int x = 0 ; x < hashsize; x++){
       int y = HASH(tmpcombco[i],x,hashsize);
       if (tax_dict_ar[t][y] == 0){
        tax_dict_ar[t][y] = tmpcombco[i];
        break;
       }
       else if (tax_dict_ar[t][y] == tmpcombco[i])
        break;
       if (x==hashsize -1){
        printf("grouping_genomes(): hashtable overflow! taxn=%d\tgid=%d\tkmer=%u\n",t,gid,tmpcombco[i]);
       }
      }
          }
        }
  }
  sprintf(tmppath,"%s/%s.%d",set_opt.outdir,skch_prefix,c);
    if((tmpfh = fopen(tmppath,"wb")) == NULL) err(errno,"grouping_genomes():%s",tmppath);
  outfn = 0;
  size_t offset = 0;
  outcombcoidx[0] = 0;
  for(int t = 0; t < subset->taxn; t++){
   if(subset->tax[t].taxid == 0) continue;
   for (int x = 0; x < tax_dict_size[t]; x++){
    if(tax_dict_ar[t][x] != 0){
     fwrite(&tax_dict_ar[t][x],sizeof(unsigned int),1,tmpfh);
     offset++;
     all_ctx_ct++;
     ctx_ct_list[outfn]++;
    }
   }
   outfn++;
   outcombcoidx[outfn]= offset;
   printf("%d/%d species pangenome grouped\r",t,subset->taxn);
   free(tax_dict_ar[t]);
  }
    printf("\n");
    fclose(tmpfh);
    sprintf(tmppath,"%s/%s.%d",set_opt.outdir,idx_prefix,c);
    tmpfh = fopen(tmppath,"wb");
    if(tmpfh == NULL) err(errno,"grouping_genomes():%s",tmppath);
    fwrite(outcombcoidx,sizeof(size_t),outfn+1,tmpfh);
    fclose(tmpfh);
    free(tmpcombco);
    free(tmpcombcoidx);
  }
  co_dstat_readin.infile_num = outfn;
  co_dstat_readin.koc = 0;
  co_dstat_readin.all_ctx_ct = all_ctx_ct;
  sprintf(tmppath,"%s/%s",set_opt.outdir,co_dstat);
  if((tmpfh = fopen(tmppath,"wb")) == NULL) err(errno,"grouping_genomes():%s",tmppath);
  fwrite(&co_dstat_readin,sizeof(co_dstat_t),1,tmpfh);
  fwrite(ctx_ct_list,sizeof(ctx_obj_ct_t),outfn,tmpfh);
  char (*tmpfname)[PATHLEN] = malloc(outfn * PATHLEN);
  int idx = 0;
  for(int t=0;t<subset->taxn;t++){
    if(subset->tax[t].taxid != 0) {
      if(subset->tax[t].taxname != NULL)
        sprintf(tmpfname[idx],"%d_%s",subset->tax[t].taxid,subset->tax[t].taxname);
      else sprintf(tmpfname[idx],"%d",subset->tax[t].taxid);
      idx++;
    }
    free(subset->tax[t].gids);
    free(subset->tax[t].taxname);
  }
  free(subset->tax);
  free(subset);
  fwrite(tmpfname,PATHLEN,outfn,tmpfh);
  fclose(tmpfh);
  free(tmpfname);
  free(ctx_ct_list);
  free(tax_dict_ar);
 free(tax_dict_size);
  free(outcombcoidx);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
movq %rdi, %rbx
callq 0xf4a6
movq %rax, %r15
leaq 0xed52(%rip), %rdi      # 0x1c688
leaq 0x6e23(%rip), %rsi      # 0x14760
callq 0x68a5
testq %rax, %rax
je 0xe06c
movq %rax, %r14
movq %r15, 0x8(%rsp)
leaq 0x670d(%rip), %rsi      # 0x14067
movq %rax, %rdi
callq 0x33a0
testq %rax, %rax
je 0xe08f
movq %rax, %r15
leaq 0x80(%rsp), %r12
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
movq %r12, %rdi
movq %rax, %rcx
callq 0x30c0
movq %r15, %rdi
callq 0x3100
movl 0x14(%r12), %r15d
movq 0x8(%rsp), %r12
movl 0x4(%r12), %ebp
cmpl %ebp, %r15d
jne 0xe0a2
movq %r14, %rdi
callq 0x3040
leaq 0xefd2(%rip), %rdi      # 0x1c988
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x3090
movl (%r12), %ebp
movslq %ebp, %rbx
pushq $0x4
popq %rsi
movq %rbx, %rdi
callq 0x32a0
movq %rax, 0x38(%rsp)
leaq (,%rbx,8), %rdi
callq 0x3340
movq %rax, 0x18(%rsp)
leaq (,%rbx,4), %rdi
callq 0x3340
movq %rax, 0x28(%rsp)
leaq 0x8(,%rbx,8), %rdi
callq 0x3340
xorl %edx, %edx
testl %ebx, %ebx
movl $0x0, %ecx
movl %ebp, 0x5c(%rsp)
cmovgl %ebp, %ecx
movq %rcx, 0x30(%rsp)
movl 0x90(%rsp), %ecx
testl %ecx, %ecx
cmovlel %edx, %ecx
movl %ecx, 0x58(%rsp)
movq %rax, 0x10(%rsp)
xorl %r8d, %r8d
movq $0x0, 0x60(%rsp)
xorl %r15d, %r15d
cmpl 0x58(%rsp), %r8d
je 0xdec3
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x6600(%rip), %rsi      # 0x1405e
leaq 0xec23(%rip), %rdx      # 0x1c688
leaq 0x8510(%rip), %rcx      # 0x15f7c
movl %r8d, 0x4(%rsp)
xorl %eax, %eax
callq 0x3430
movq %rbx, %rdi
leaq 0x1a8(%rsp), %rsi
callq 0x32e0
testl %eax, %eax
jne 0xe04f
movq 0x1d8(%rsp), %r14
movq %r14, %rdi
callq 0x3340
movq %rax, %r13
movq %rbx, %rdi
leaq 0x65ba(%rip), %rsi      # 0x14067
callq 0x33a0
testq %rax, %rax
je 0xe04f
movq %rax, %r15
movq %r13, 0x48(%rsp)
movq %r13, %rdi
movq %r14, %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x30c0
movq %r15, %rdi
callq 0x3100
movq %rbx, %rdi
leaq 0x6578(%rip), %rsi      # 0x1405e
leaq 0xeb9b(%rip), %rdx      # 0x1c688
leaq 0x848f(%rip), %rcx      # 0x15f83
movl 0x4(%rsp), %r8d
xorl %eax, %eax
callq 0x3430
movq %rbx, %rdi
leaq 0x1a8(%rsp), %rsi
callq 0x32e0
testl %eax, %eax
jne 0xe04f
movq 0x1d8(%rsp), %r14
movq %r14, %rdi
callq 0x3340
movq %rax, %r13
movq %rbx, %rdi
leaq 0x6532(%rip), %rsi      # 0x14067
callq 0x33a0
testq %rax, %rax
je 0xe04f
movq %rax, %r15
movq %r13, 0x40(%rsp)
movq %r13, %rdi
movq %r14, %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x30c0
movq %r15, %rdi
callq 0x3100
xorl %r9d, %r9d
cmpq 0x30(%rsp), %r9
je 0xdd1e
movq 0x8(%r12), %rax
imulq $0x18, %r9, %rcx
cmpl $0x0, (%rax,%rcx)
je 0xdd11
addq %rcx, %rax
addq $0x10, %rax
movq %rax, 0x68(%rsp)
movq (%rax), %r15
movl (%r15), %r13d
xorl %eax, %eax
testl %r13d, %r13d
movl $0x0, %ecx
cmovgl %r13d, %ecx
xorl %edx, %edx
movq 0x40(%rsp), %r8
cmpq %rax, %rcx
je 0xdbc6
movslq 0x4(%r15,%rax,4), %rsi
movl 0x8(%r8,%rsi,8), %edi
subl (%r8,%rsi,8), %edi
addl %edi, %edx
incq %rax
jmp 0xdbac
cvtsi2sd %edx, %xmm0
mulsd 0x8356(%rip), %xmm0     # 0x15f28
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd 0x6b2e(%rip), %xmm0     # 0x14710
cvttsd2si %xmm0, %rdx
sarq $0x3f, %rcx
andq %rcx, %rdx
orq %rax, %rdx
bsrq %rdx, %rax
movl %eax, %ecx
xorl $0x3f, %ecx
pushq $0x38
popq %rdx
subl %ecx, %edx
xorl $0x7, %eax
cmpl $0x8, %eax
movl $0x0, %eax
cmovbq %rax, %rdx
leaq 0x90ab(%rip), %rax      # 0x16cc0
movl (%rax,%rdx,4), %ebp
movq 0x28(%rsp), %rax
movl %ebp, (%rax,%r9,4)
movslq %ebp, %rbx
movq %rbx, %rdi
pushq $0x4
popq %rsi
movq %r9, 0x20(%rsp)
callq 0x32a0
movq 0x20(%rsp), %r9
movq %rax, %r14
movq 0x18(%rsp), %rax
movq %r14, (%rax,%r9,8)
leal -0x1(%rbx), %r12d
testl %ebx, %ebx
movl $0x0, %ebx
cmovgl %ebp, %ebx
pushq $0x1
popq %rcx
movq 0x48(%rsp), %rdi
movslq %r13d, %rax
cmpq %rax, %rcx
jg 0xdd11
movq %rcx, 0x70(%rsp)
movslq (%r15,%rcx,4), %rcx
movq 0x40(%rsp), %rax
movq (%rax,%rcx,8), %r13
movq %rcx, 0x50(%rsp)
movq 0x8(%rax,%rcx,8), %rax
movq %rax, 0x78(%rsp)
cmpq 0x78(%rsp), %r13
jae 0xdcf9
xorl %r15d, %r15d
cmpl %r15d, %ebx
je 0xdcf4
movl (%rdi,%r13,4), %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %ebp
movl %edx, %esi
movl %ecx, %eax
xorl %edx, %edx
divl %r12d
leal 0x1(%rdx), %eax
imull %r15d, %eax
addl %esi, %eax
xorl %edx, %edx
divl %ebp
movslq %edx, %rax
movl (%r14,%rax,4), %edx
testl %edx, %edx
je 0xdcf0
cmpl %ecx, %edx
je 0xdcf4
cmpl %r15d, %r12d
jne 0xdceb
leaq 0x85a6(%rip), %rdi      # 0x16278
movl %r9d, %esi
movq 0x50(%rsp), %rdx
xorl %eax, %eax
callq 0x3170
movq 0x20(%rsp), %r9
movq 0x48(%rsp), %rdi
incl %r15d
jmp 0xdc92
movl %ecx, (%r14,%rax,4)
incq %r13
jmp 0xdc88
movq 0x70(%rsp), %rcx
incq %rcx
movq 0x68(%rsp), %rax
movq (%rax), %r15
movl (%r15), %r13d
jmp 0xdc5b
incq %r9
movq 0x8(%rsp), %r12
jmp 0xdb67
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x632e(%rip), %rsi      # 0x1405e
leaq 0xec51(%rip), %rdx      # 0x1c988
leaq 0x823e(%rip), %rcx      # 0x15f7c
movl 0x4(%rsp), %r8d
xorl %eax, %eax
callq 0x3430
movq %rbx, %rdi
leaq 0x6316(%rip), %rsi      # 0x1406a
callq 0x33a0
testq %rax, %rax
je 0xe04f
movq %rax, %r13
movq 0x10(%rsp), %rax
andq $0x0, (%rax)
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %ebp, %ebp
cmpq 0x30(%rsp), %r14
je 0xde2d
movq 0x8(%r12), %rax
imulq $0x18, %r14, %rcx
cmpl $0x0, (%rax,%rcx)
je 0xde25
movq 0x28(%rsp), %rax
movl (%rax,%r14,4), %r12d
movslq %r15d, %rbx
testl %r12d, %r12d
movl $0x0, %eax
cmovlel %eax, %r12d
shlq $0x2, %r12
xorl %r15d, %r15d
cmpq %r15, %r12
je 0xddef
movq 0x18(%rsp), %rax
movq (%rax,%r14,8), %rdi
cmpl $0x0, (%rdi,%r15)
je 0xdde9
addq %r15, %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r13, %rcx
callq 0x3450
incq %rbp
incq 0x60(%rsp)
movq 0x38(%rsp), %rax
incl (%rax,%rbx,4)
addq $0x4, %r15
jmp 0xddb3
leal 0x1(%rbx), %r15d
movq 0x10(%rsp), %rax
movq %rbp, 0x8(%rax,%rbx,8)
leaq 0x83ec(%rip), %rdi      # 0x161f0
movl %r14d, %esi
movl 0x5c(%rsp), %edx
xorl %eax, %eax
callq 0x3170
movq 0x18(%rsp), %rax
movq (%rax,%r14,8), %rdi
callq 0x3040
movq 0x8(%rsp), %r12
incq %r14
jmp 0xdd76
pushq $0xa
popq %rdi
callq 0x3050
movq %r13, %rdi
callq 0x3100
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x620f(%rip), %rsi      # 0x1405e
leaq 0xeb32(%rip), %rdx      # 0x1c988
leaq 0x8126(%rip), %rcx      # 0x15f83
movl 0x4(%rsp), %ebp
movl %ebp, %r8d
xorl %eax, %eax
callq 0x3430
movq %rbx, %rdi
leaq 0x61f5(%rip), %rsi      # 0x1406a
callq 0x33a0
testq %rax, %rax
je 0xe04f
movq %rax, %r14
leal 0x1(%r15), %eax
movslq %eax, %rdx
movq 0x10(%rsp), %rdi
pushq $0x8
popq %rsi
movq %r14, %rcx
callq 0x3450
movq %r14, %rdi
callq 0x3100
movq 0x48(%rsp), %rdi
callq 0x3040
movq 0x40(%rsp), %rdi
callq 0x3040
incl %ebp
movl %ebp, %r8d
jmp 0xda41
movl %r15d, 0x94(%rsp)
movb $0x0, 0x84(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x98(%rsp)
leaq 0x64ac(%rip), %rsi      # 0x14393
leaq 0xea9a(%rip), %rdx      # 0x1c988
leaq 0x686b(%rip), %rcx      # 0x14760
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3430
leaq 0x615c(%rip), %rsi      # 0x1406a
movq %rbx, %rdi
callq 0x33a0
testq %rax, %rax
je 0xe04f
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x3450
movslq %r15d, %rdx
pushq $0x4
popq %rsi
movq 0x38(%rsp), %rdi
movq %rdx, 0x20(%rsp)
movq %rbx, 0x50(%rsp)
movq %rbx, %rcx
callq 0x3450
shll $0x8, %r15d
movslq %r15d, %rdi
callq 0x3340
movq %rax, %r12
movq 0x8(%rsp), %rax
movq 0x8(%rax), %r15
imulq $0x18, 0x30(%rsp), %r14
leaq 0x8297(%rip), %rbp      # 0x16211
xorl %ebx, %ebx
xorl %r13d, %r13d
cmpq %rbx, %r14
je 0xdfd7
movl (%r15,%rbx), %edx
testl %edx, %edx
je 0xdfbd
movq 0x8(%r15,%rbx), %rcx
movslq %r13d, %rdi
shlq $0x8, %rdi
addq %r12, %rdi
testq %rcx, %rcx
je 0xdfac
movq %rbp, %rsi
xorl %eax, %eax
callq 0x3430
jmp 0xdfba
leaq 0x91e0(%rip), %rsi      # 0x17193
xorl %eax, %eax
callq 0x3430
incl %r13d
movq 0x10(%r15,%rbx), %rdi
callq 0x3040
movq 0x8(%r15,%rbx), %rdi
callq 0x3040
addq $0x18, %rbx
jmp 0xdf7f
movq %r15, %rdi
callq 0x3040
movq 0x8(%rsp), %rdi
callq 0x3040
movl $0x100, %esi            # imm = 0x100
movq %r12, %rdi
movq 0x20(%rsp), %rdx
movq 0x50(%rsp), %rbx
movq %rbx, %rcx
callq 0x3450
movq %rbx, %rdi
callq 0x3100
movq %r12, %rdi
callq 0x3040
movq 0x38(%rsp), %rdi
callq 0x3040
movq 0x18(%rsp), %rdi
callq 0x3040
movq 0x28(%rsp), %rdi
callq 0x3040
movq 0x10(%rsp), %rdi
callq 0x3040
xorl %eax, %eax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3060
movl (%rax), %edi
leaq 0x81ba(%rip), %rsi      # 0x16217
leaq 0xa0(%rsp), %rdx
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x6353(%rip), %rsi      # 0x143cd
leaq 0x66df(%rip), %rdx      # 0x14760
leaq 0xe600(%rip), %rcx      # 0x1c688
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x817a(%rip), %rsi      # 0x16217
movq %r14, %rdx
jmp 0xe065
callq 0x3060
movl (%rax), %edi
leaq 0x817d(%rip), %rsi      # 0x1622d
movq %r14, %rdx
movl %r15d, %ecx
movq %rbx, %r8
movl %ebp, %r9d
xorl %eax, %eax
callq 0x3330