// File: /workspace/github_c_makefile_star2_O1/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 0xff54
movq %rax, 0x8(%rsp)
leaq 0xf398(%rip), %rdi      # 0x1d688
leaq 0x7469(%rip), %rsi      # 0x15760
callq 0x6bcc
testq %rax, %rax
je 0xead1
movq %rax, %r14
leaq 0x6d58(%rip), %rsi      # 0x15067
movq %rax, %rdi
callq 0x33a0
testq %rax, %rax
je 0xeaf4
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 0xeb07
movq %r14, %rdi
callq 0x3040
leaq 0xf619(%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 0xe90a
leaq 0xf29f(%rip), %r13      # 0x1d688
leaq 0xc0(%rsp), %rbp
movq $0x0, 0x48(%rsp)
xorl %r8d, %r8d
movq %rbp, %rdi
leaq 0x6c57(%rip), %rsi      # 0x1505e
movq %r13, %rdx
leaq 0x8b6b(%rip), %rcx      # 0x16f7c
movl %r8d, 0x4(%rsp)
xorl %eax, %eax
callq 0x3430
movq %rbp, %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x32e0
testl %eax, %eax
jne 0xeab4
movq 0x1f8(%rsp), %rbx
movq %rbx, %rdi
callq 0x3340
movq %rax, %r15
movq %rbp, %rdi
leaq 0x6c15(%rip), %rsi      # 0x15067
callq 0x33a0
testq %rax, %rax
je 0xeab4
movq %rax, %r14
movl $0x1, %edx
movq %r15, 0x60(%rsp)
movq %r15, %rdi
movq %rbx, %rsi
movq %rax, %rcx
callq 0x30c0
movq %r14, %rdi
callq 0x3100
movq %rbp, %rdi
leaq 0x6bd1(%rip), %rsi      # 0x1505e
movq %r13, %rdx
leaq 0x8aec(%rip), %rcx      # 0x16f83
movl 0x4(%rsp), %r8d
xorl %eax, %eax
callq 0x3430
movq %rbp, %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x32e0
testl %eax, %eax
jne 0xeab4
movq 0x1f8(%rsp), %rbx
movq %rbx, %rdi
callq 0x3340
movq %rax, %r13
movq %rbp, %rdi
leaq 0x6b8f(%rip), %rsi      # 0x15067
callq 0x33a0
testq %rax, %rax
je 0xeab4
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 0xe73c
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x40(%rsp)
xorl %r10d, %r10d
leaq (%r10,%r10,2), %rax
movq 0x40(%rsp), %rcx
cmpl $0x0, (%rcx,%rax,8)
je 0xe72b
movq 0x40(%rsp), %rcx
movq 0x10(%rcx,%rax,8), %rcx
movl (%rcx), %edx
testl %edx, %edx
jle 0xe599
incq %rdx
xorl %esi, %esi
movl $0x1, %edi
movslq (%rcx,%rdi,4), %r8
movl 0x8(%r13,%r8,8), %r9d
subl (%r13,%r8,8), %r9d
addl %r9d, %esi
incq %rdi
cmpq %rdi, %rdx
jne 0xe550
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
mulsd 0x89b0(%rip), %xmm0     # 0x16f28
cvttsd2si %xmm0, %rdx
movq %rdx, %rsi
sarq $0x3f, %rsi
subsd 0x7184(%rip), %xmm0     # 0x15710
cvttsd2si %xmm0, %rcx
andq %rsi, %rcx
orq %rdx, %rcx
jmp 0xe59b
xorl %ecx, %ecx
bsrq %rcx, %rcx
movl %ecx, %edx
xorl $0x3f, %edx
movl $0x38, %esi
subl %edx, %esi
xorl $0x7, %ecx
cmpl $0x8, %ecx
movl $0x0, %ecx
cmovbq %rcx, %rsi
movq 0x40(%rsp), %rcx
leaq 0x10(%rcx,%rax,8), %rbx
leaq 0x9715(%rip), %rax      # 0x17ce0
movl (%rax,%rsi,4), %r14d
movq 0x30(%rsp), %rax
movl %r14d, (%rax,%r10,4)
movslq %r14d, %rdi
movl $0x4, %esi
movq %r10, 0x28(%rsp)
callq 0x32a0
movq 0x28(%rsp), %r10
movq %rax, %r8
movq 0x20(%rsp), %rax
movq %r8, (%rax,%r10,8)
movq %rbx, 0x80(%rsp)
movq (%rbx), %rax
cmpl $0x0, (%rax)
movq 0x60(%rsp), %rsi
jle 0xe72b
movq %r14, %rdi
leal -0x1(%r14), %ecx
movl %ecx, 0x6c(%rsp)
movl $0x1, %ecx
movq %r14, 0x98(%rsp)
movq %rcx, 0x88(%rsp)
movslq (%rax,%rcx,4), %rax
movq (%r13,%rax,8), %rcx
movq %rax, 0x10(%rsp)
movq 0x8(%r13,%rax,8), %rdx
movq %rcx, 0x58(%rsp)
movq %rdx, 0x90(%rsp)
cmpq %rdx, %rcx
jae 0xe703
testl %edi, %edi
jle 0xe6e8
movq 0x58(%rsp), %rax
movl (%rsi,%rax,4), %r14d
xorl %ebp, %ebp
movl %r14d, %eax
xorl %edx, %edx
divl %edi
movl %edx, %ebx
movl %r14d, %eax
xorl %edx, %edx
divl 0x6c(%rsp)
movl %edx, %r12d
incl %r12d
movl %edi, %r15d
movl %r12d, %eax
imull %ebp, %eax
addl %ebx, %eax
xorl %edx, %edx
divl %edi
movl (%r8,%rdx,4), %eax
testl %eax, %eax
je 0xe6e4
cmpl %r14d, %eax
je 0xe6e8
cmpl $0x1, %r15d
jne 0xe6db
leaq 0x8bc7(%rip), %rdi      # 0x17278
movl %r10d, %esi
movq 0x10(%rsp), %rdx
movl %r14d, %ecx
xorl %eax, %eax
movq %r8, %r13
callq 0x3170
movq %r13, %r8
movq 0x98(%rsp), %rdi
movq 0x28(%rsp), %r10
movq 0x60(%rsp), %rsi
incl %ebp
decl %r15d
jne 0xe68b
jmp 0xe6e8
movl %r14d, (%r8,%rdx,4)
movq 0x58(%rsp), %rcx
incq %rcx
movq %rcx, 0x58(%rsp)
cmpq 0x90(%rsp), %rcx
jne 0xe65d
movq 0x88(%rsp), %r9
leaq 0x1(%r9), %rcx
movq 0x80(%rsp), %rax
movq (%rax), %rax
movslq (%rax), %rdx
cmpq %rdx, %r9
movq 0x70(%rsp), %r13
jl 0xe62c
incq %r10
movq 0x38(%rsp), %r12
cmpq %r12, %r10
jne 0xe523
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x6910(%rip), %rsi      # 0x1505e
leaq 0xf233(%rip), %rdx      # 0x1d988
leaq 0x8820(%rip), %rcx      # 0x16f7c
movl 0x4(%rsp), %r8d
xorl %eax, %eax
callq 0x3430
movq %rbx, %rdi
leaq 0x68f8(%rip), %rsi      # 0x1506a
callq 0x33a0
testq %rax, %rax
je 0xeab4
movq %rax, %r15
movq 0x18(%rsp), %rax
movq $0x0, (%rax)
testl %r12d, %r12d
jle 0xe857
movq 0x8(%rsp), %rax
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 0xe849
movq 0x30(%rsp), %rax
movslq (%rax,%rbp,4), %r12
movslq %ebx, %r14
testq %r12, %r12
jle 0xe815
movq 0x20(%rsp), %rax
movq (%rax,%rbp,8), %rbx
cmpl $0x0, (%rbx)
je 0xe80c
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 0xe7e1
leal 0x1(%r14), %ebx
movq 0x18(%rsp), %rax
movq %r13, 0x8(%rax,%r14,8)
leaq 0x89c6(%rip), %rdi      # 0x171f0
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
incq %rbp
cmpq %r12, %rbp
jne 0xe7ad
jmp 0xe859
xorl %ebx, %ebx
movq %rbx, %r13
movl $0xa, %edi
callq 0x3050
movq %r15, %rdi
callq 0x3100
leaq 0xc0(%rsp), %rbp
movq %rbp, %rdi
leaq 0x67de(%rip), %rsi      # 0x1505e
leaq 0xf101(%rip), %rdx      # 0x1d988
leaq 0x86f5(%rip), %rcx      # 0x16f83
movl 0x4(%rsp), %r14d
movl %r14d, %r8d
xorl %eax, %eax
callq 0x3430
movq %rbp, %rdi
leaq 0x67c3(%rip), %rsi      # 0x1506a
callq 0x33a0
testq %rax, %rax
je 0xeab4
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 0x60(%rsp), %rdi
callq 0x3040
movq 0x70(%rsp), %rdi
callq 0x3040
incl %r14d
cmpl 0x68(%rsp), %r14d
leaq 0xed89(%rip), %r13      # 0x1d688
movl %r14d, %r8d
jne 0xe3fd
jmp 0xe916
movq $0x0, 0x48(%rsp)
xorl %r15d, %r15d
movl %r15d, 0xb4(%rsp)
movb $0x0, 0xa4(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0xb8(%rsp)
leaq 0x6a59(%rip), %rsi      # 0x15393
leaq 0xf047(%rip), %rdx      # 0x1d988
leaq 0x6e18(%rip), %rcx      # 0x15760
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3430
leaq 0x6709(%rip), %rsi      # 0x1506a
movq %rbx, %rdi
callq 0x33a0
testq %rax, %rax
je 0xeab4
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
cmpl $0x0, 0x38(%rsp)
jle 0xea38
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rbp
movq 0x78(%rsp), %rax
leaq (%rax,%rax,2), %r15
leaq 0x8834(%rip), %r13      # 0x17211
xorl %r14d, %r14d
xorl %ebx, %ebx
movl (%rbp,%r14), %edx
testl %edx, %edx
je 0xea1b
movq 0x8(%rbp,%r14), %rcx
movslq %ebx, %rdi
shlq $0x8, %rdi
addq %r12, %rdi
testq %rcx, %rcx
je 0xea0b
movq %r13, %rsi
xorl %eax, %eax
callq 0x3430
jmp 0xea19
leaq 0x97a1(%rip), %rsi      # 0x181b3
xorl %eax, %eax
callq 0x3430
incl %ebx
movq 0x10(%rbp,%r14), %rdi
callq 0x3040
movq 0x8(%rbp,%r14), %rdi
callq 0x3040
addq $0x18, %r14
cmpq %r14, %r15
jne 0xe9e2
movq 0x8(%rsp), %rbx
movq 0x8(%rbx), %rdi
callq 0x3040
movq %rbx, %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 0x8755(%rip), %rsi      # 0x17217
leaq 0xc0(%rsp), %rdx
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x68ee(%rip), %rsi      # 0x153cd
leaq 0x6c7a(%rip), %rdx      # 0x15760
leaq 0xeb9b(%rip), %rcx      # 0x1d688
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x8715(%rip), %rsi      # 0x17217
movq %r14, %rdx
jmp 0xeaca
callq 0x3060
movl (%rax), %edi
leaq 0x8718(%rip), %rsi      # 0x1722d
movq %r14, %rdx
movl %r15d, %ecx
movq %rbx, %r8
movl %ebp, %r9d
xorl %eax, %eax
callq 0x3330