// File: /workspace/github_c_makefile_star2_O3/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 $0x258, %rsp            # imm = 0x258
movq %rdi, %rbx
callq 0x102ac
movq %rax, 0x8(%rsp)
leaq 0xf0ab(%rip), %rdi      # 0x1d688
leaq 0x71cc(%rip), %rsi      # 0x157b0
callq 0x6d79
testq %rax, %rax
je 0xedc4
movq %rax, %r14
leaq 0x6a6b(%rip), %rsi      # 0x15067
movq %rax, %rdi
callq 0x33a0
testq %rax, %rax
je 0xede7
movq %rax, %r15
leaq 0xa0(%rsp), %r12
movl $0x20, %esi
movl $0x1, %edx
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 0xedfa
movq %r14, %rdi
callq 0x3040
leaq 0xf32c(%rip), %rdi      # 0x1d988
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x3090
movslq (%r12), %r12
movl $0x4, %esi
movq %r12, %rdi
callq 0x32a0
movq %rax, 0x50(%rsp)
leaq (,%r12,8), %rdi
movq %rdi, 0x78(%rsp)
callq 0x3340
movq %rax, 0x20(%rsp)
leaq (,%r12,4), %rdi
callq 0x3340
movq %rax, 0x30(%rsp)
leaq 0x8(,%r12,8), %rdi
callq 0x3340
movq %rax, 0x18(%rsp)
movl 0xb0(%rsp), %eax
movl %eax, 0x68(%rsp)
testl %eax, %eax
movq %r12, 0x38(%rsp)
jle 0xec02
leaq 0xc0(%rsp), %rdi
movq $0x0, 0x48(%rsp)
xorl %r8d, %r8d
leaq 0x6974(%rip), %rsi      # 0x1505e
leaq 0xef97(%rip), %rdx      # 0x1d688
leaq 0x88d4(%rip), %rcx      # 0x16fcc
movl %r8d, 0x4(%rsp)
movq %rdi, %r15
xorl %eax, %eax
callq 0x3430
movq %r15, %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x32e0
testl %eax, %eax
jne 0xeda7
movq 0x1f8(%rsp), %rbx
movq %rbx, %rdi
callq 0x3340
movq %rax, %r13
movq %r15, %rdi
leaq 0x692b(%rip), %rsi      # 0x15067
callq 0x33a0
testq %rax, %rax
je 0xeda7
movq %rax, %r14
movl $0x1, %edx
movq %r13, 0x58(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
movq %rax, %rcx
callq 0x30c0
movq %r14, %rdi
callq 0x3100
movq %r15, %rdi
leaq 0x68e7(%rip), %rsi      # 0x1505e
leaq 0xef0a(%rip), %rdx      # 0x1d688
leaq 0x884e(%rip), %rcx      # 0x16fd3
movl 0x4(%rsp), %r8d
xorl %eax, %eax
callq 0x3430
movq %r15, %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x32e0
testl %eax, %eax
jne 0xeda7
movq 0x1f8(%rsp), %rbx
movq %rbx, %rdi
callq 0x3340
movq %rax, %r13
movq %r15, %rdi
leaq 0x68a1(%rip), %rsi      # 0x15067
callq 0x33a0
testq %rax, %rax
je 0xeda7
movq %rax, %r14
movl $0x1, %edx
movq %r13, %rdi
movq %rbx, %rsi
movq %rax, %rcx
callq 0x30c0
movq %r14, %rdi
callq 0x3100
testl %r12d, %r12d
movq %r13, 0x70(%rsp)
jle 0xea2e
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x40(%rsp)
xorl %r8d, %r8d
leaq (%r8,%r8,2), %r14
movq 0x40(%rsp), %rax
cmpl $0x0, (%rax,%r14,8)
je 0xea1d
movq 0x40(%rsp), %rax
movq 0x10(%rax,%r14,8), %rbx
movl (%rbx), %r12d
testl %r12d, %r12d
jle 0xe88b
leaq 0x1(%r12), %rcx
xorl %eax, %eax
movl $0x1, %edx
movslq (%rbx,%rdx,4), %rsi
movl 0x8(%r13,%rsi,8), %edi
subl (%r13,%rsi,8), %edi
addl %edi, %eax
incq %rdx
cmpq %rdx, %rcx
jne 0xe843
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x870e(%rip), %xmm0     # 0x16f78
cvttsd2si %xmm0, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
subsd 0x6ee2(%rip), %xmm0     # 0x15760
cvttsd2si %xmm0, %rax
andq %rdx, %rax
orq %rcx, %rax
jmp 0xe88d
xorl %eax, %eax
bsrq %rax, %rax
movl %eax, %ecx
xorl $0x3f, %ecx
movl $0x38, %edx
subl %ecx, %edx
xorl $0x7, %eax
cmpl $0x8, %eax
movl $0x0, %eax
cmovbq %rax, %rdx
leaq 0x945d(%rip), %rax      # 0x17d10
movl (%rax,%rdx,4), %r15d
movq 0x30(%rsp), %rax
movl %r15d, (%rax,%r8,4)
movslq %r15d, %rdi
movl $0x4, %esi
movq %r8, 0x28(%rsp)
callq 0x32a0
movq 0x28(%rsp), %r8
movq %rax, %rdi
movq 0x20(%rsp), %rax
movq %rdi, (%rax,%r8,8)
testl %r12d, %r12d
jle 0xea1d
movq %r15, %rsi
movq 0x40(%rsp), %rax
leaq (%rax,%r14,8), %rax
addq $0x10, %rax
movq %rax, 0x80(%rsp)
leal -0x1(%r15), %eax
movl %eax, 0x6c(%rsp)
movl $0x1, %edx
movq %rdi, 0x98(%rsp)
movslq (%rbx,%rdx,4), %rax
movq (%r13,%rax,8), %rcx
movq %rax, 0x10(%rsp)
movq 0x8(%r13,%rax,8), %r9
movq %rcx, 0x60(%rsp)
movq %r9, 0x90(%rsp)
cmpq %r9, %rcx
jae 0xea0a
movq %rdx, 0x88(%rsp)
testl %esi, %esi
jle 0xe9d4
movq 0x58(%rsp), %rax
movq 0x60(%rsp), %rcx
movl (%rax,%rcx,4), %r13d
xorl %ebp, %ebp
movl %r13d, %eax
xorl %edx, %edx
divl %esi
movl %edx, %ebx
movl %r13d, %eax
xorl %edx, %edx
divl 0x6c(%rsp)
movl %edx, %r12d
incl %r12d
movl %esi, %r15d
movl %r12d, %eax
imull %ebp, %eax
addl %ebx, %eax
xorl %edx, %edx
divl %esi
movl (%rdi,%rdx,4), %eax
testl %eax, %eax
je 0xe9d0
cmpl %r13d, %eax
je 0xe9d4
cmpl $0x1, %r15d
jne 0xe9c7
leaq 0x8926(%rip), %rdi      # 0x172c8
movq %rsi, %r14
movl %r8d, %esi
movq 0x10(%rsp), %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x3170
movq 0x98(%rsp), %rdi
movq %r14, %rsi
movq 0x28(%rsp), %r8
incl %ebp
decl %r15d
jne 0xe97d
jmp 0xe9d4
movl %r13d, (%rdi,%rdx,4)
movq 0x60(%rsp), %rcx
incq %rcx
movq %rcx, 0x60(%rsp)
cmpq 0x90(%rsp), %rcx
jne 0xe94a
movq 0x80(%rsp), %rax
movq (%rax), %rbx
movl (%rbx), %r12d
movq 0x70(%rsp), %r13
movq 0x88(%rsp), %rdx
leaq 0x1(%rdx), %rax
movslq %r12d, %rcx
cmpq %rcx, %rdx
movq %rax, %rdx
jl 0xe919
incq %r8
movq 0x38(%rsp), %r12
cmpq %r12, %r8
jne 0xe811
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x661e(%rip), %rsi      # 0x1505e
leaq 0xef41(%rip), %rdx      # 0x1d988
leaq 0x857e(%rip), %rcx      # 0x16fcc
movl 0x4(%rsp), %r8d
xorl %eax, %eax
callq 0x3430
movq %rbx, %rdi
leaq 0x6606(%rip), %rsi      # 0x1506a
callq 0x33a0
testq %rax, %rax
je 0xeda7
movq %rax, %r15
movq 0x18(%rsp), %rax
movq $0x0, (%rax)
testl %r12d, %r12d
movq 0x8(%rsp), %rax
movq 0x58(%rsp), %r14
jle 0xeb53
movq 0x8(%rax), %rax
movq %rax, 0x10(%rsp)
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %ebx, %ebx
leaq (,%rbp,2), %rax
addq %rbp, %rax
movq 0x10(%rsp), %rcx
cmpl $0x0, (%rcx,%rax,8)
je 0xeb45
movq 0x30(%rsp), %rax
movslq (%rax,%rbp,4), %r12
movslq %ebx, %r14
testq %r12, %r12
jle 0xeb0c
movq 0x20(%rsp), %rax
movq (%rax,%rbp,8), %rbx
cmpl $0x0, (%rbx)
je 0xeb03
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rcx
callq 0x3450
incq %r13
incq 0x48(%rsp)
movq 0x50(%rsp), %rax
incl (%rax,%r14,4)
addq $0x4, %rbx
decq %r12
jne 0xead8
leal 0x1(%r14), %ebx
movq 0x18(%rsp), %rax
movq %r13, 0x8(%rax,%r14,8)
leaq 0x871f(%rip), %rdi      # 0x17240
movl %ebp, %esi
movq 0x38(%rsp), %r12
movl %r12d, %edx
xorl %eax, %eax
callq 0x3170
movq 0x20(%rsp), %rax
movq (%rax,%rbp,8), %rdi
callq 0x3040
movq 0x58(%rsp), %r14
incq %rbp
cmpq %r12, %rbp
jne 0xeaa4
jmp 0xeb55
xorl %ebx, %ebx
movq %rbx, %r13
movl $0xa, %edi
callq 0x3050
movq %r15, %rdi
callq 0x3100
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x64e2(%rip), %rsi      # 0x1505e
leaq 0xee05(%rip), %rdx      # 0x1d988
leaq 0x8449(%rip), %rcx      # 0x16fd3
movl 0x4(%rsp), %ebp
movl %ebp, %r8d
xorl %eax, %eax
callq 0x3430
movq %rbx, %rdi
leaq 0x64c8(%rip), %rsi      # 0x1506a
callq 0x33a0
testq %rax, %rax
je 0xeda7
movq %rax, %rbx
movq %r13, %r15
leal 0x1(%r13), %eax
movslq %eax, %rdx
movl $0x8, %esi
movq 0x18(%rsp), %rdi
movq %rbx, %rcx
callq 0x3450
movq %rbx, %rdi
callq 0x3100
movq %r14, %rdi
callq 0x3040
movq 0x70(%rsp), %rdi
callq 0x3040
incl %ebp
cmpl 0x68(%rsp), %ebp
leaq 0xc0(%rsp), %rdi
movl %ebp, %r8d
jne 0xe6e3
jmp 0xec0e
movq $0x0, 0x48(%rsp)
xorl %r15d, %r15d
movl %r15d, 0xb4(%rsp)
movb $0x0, 0xa4(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0xb8(%rsp)
leaq 0x67b1(%rip), %rsi      # 0x153e3
leaq 0xed4f(%rip), %rdx      # 0x1d988
leaq 0x6b70(%rip), %rcx      # 0x157b0
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3430
leaq 0x6411(%rip), %rsi      # 0x1506a
movq %rbx, %rdi
callq 0x33a0
testq %rax, %rax
je 0xeda7
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
movl $0x20, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x3450
movslq %r15d, %rdx
movl $0x4, %esi
movq 0x50(%rsp), %rdi
movq %rdx, 0x28(%rsp)
movq %rbx, 0x10(%rsp)
movq %rbx, %rcx
callq 0x3450
shll $0x8, %r15d
movslq %r15d, %rdi
callq 0x3340
movq %rax, %r12
movq 0x8(%rsp), %rax
movq 0x8(%rax), %r14
cmpl $0x0, 0x38(%rsp)
jle 0xed2f
movq 0x78(%rsp), %rax
leaq (%rax,%rax,2), %r15
leaq 0x858c(%rip), %rbp      # 0x17261
xorl %r13d, %r13d
xorl %ebx, %ebx
movl (%r14,%r13), %edx
testl %edx, %edx
je 0xed12
movq 0x8(%r14,%r13), %rcx
movslq %ebx, %rdi
shlq $0x8, %rdi
addq %r12, %rdi
testq %rcx, %rcx
je 0xed02
movq %rbp, %rsi
xorl %eax, %eax
callq 0x3430
jmp 0xed10
leaq 0x94da(%rip), %rsi      # 0x181e3
xorl %eax, %eax
callq 0x3430
incl %ebx
movq 0x10(%r14,%r13), %rdi
callq 0x3040
movq 0x8(%r14,%r13), %rdi
callq 0x3040
addq $0x18, %r13
cmpq %r13, %r15
jne 0xecda
movq %r14, %rdi
callq 0x3040
movq 0x8(%rsp), %rdi
callq 0x3040
movl $0x100, %esi            # imm = 0x100
movq %r12, %rdi
movq 0x28(%rsp), %rdx
movq 0x10(%rsp), %rbx
movq %rbx, %rcx
callq 0x3450
movq %rbx, %rdi
callq 0x3100
movq %r12, %rdi
callq 0x3040
movq 0x50(%rsp), %rdi
callq 0x3040
movq 0x20(%rsp), %rdi
callq 0x3040
movq 0x30(%rsp), %rdi
callq 0x3040
movq 0x18(%rsp), %rdi
callq 0x3040
xorl %eax, %eax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3060
movl (%rax), %edi
leaq 0x84b2(%rip), %rsi      # 0x17267
leaq 0xc0(%rsp), %rdx
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x664b(%rip), %rsi      # 0x1541d
leaq 0x69d7(%rip), %rdx      # 0x157b0
leaq 0xe8a8(%rip), %rcx      # 0x1d688
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x8472(%rip), %rsi      # 0x17267
movq %r14, %rdx
jmp 0xedbd
callq 0x3060
movl (%rax), %edi
leaq 0x8475(%rip), %rsi      # 0x1727d
movq %r14, %rdx
movl %r15d, %ecx
movq %rbx, %r8
movl %ebp, %r9d
xorl %eax, %eax
callq 0x3330