// File: /workspace/github_c_makefile_star2_O1/yhg926[P]public_kssd/co2mco.c
// Function: cdb_kmerf2kmerdb
void cdb_kmerf2kmerdb(const char *mcodirname, const char *codirname, int cofnum, int comp_num, int p_fit_mem)
{
 kmerdb_index_t mco_map;
 size_t comp_sz = (1LLU << 4*COMPONENT_SZ);
 int binnum = ceil((double)cofnum/BIN_SZ);
 mco_map.row_bin_gnum = malloc((size_t)comp_sz*binnum*sizeof(unsigned int));
 mco_map.row_gnum = malloc(comp_sz * sizeof(unsigned int));
 mco_map.row_offset = malloc(comp_sz*sizeof(size_t) );
 mco_map.row_offset[0] = 0;
 size_t *cbdcoindex = malloc( sizeof(size_t)*(cofnum + 1) );
 char cbdcofname[PATHLEN]; char cbdcoindexf[PATHLEN];
 char mcofname[PATHLEN]; char mcoindexf[PATHLEN];
 mmp_uint_t mmpcbd_cofile;
 gid_arr_llist_t** mco = malloc(comp_sz* sizeof(gid_arr_llist_t*));
 for(unsigned int i = 0; i< comp_num; i++){
  memset(mco_map.row_bin_gnum,0,(size_t)comp_sz*binnum*sizeof(unsigned int) );
  memset(mco_map.row_gnum,0,comp_sz * sizeof(unsigned int));
  FILE *cbdfp, *cbdindexfp;
  sprintf(cbdcoindexf,"%s/combco.index.%d",codirname,i);
  sprintf(cbdcofname,"%s/combco.%d",codirname,i);
    if( (cbdfp = fopen(cbdcofname,"rb")) == NULL) err(errno,"%s",cbdcofname);
    if( (cbdindexfp = fopen(cbdcoindexf,"rb")) == NULL) err(errno,"%s",cbdcoindexf);
  fread(cbdcoindex,sizeof(size_t),cofnum + 1,cbdindexfp);
  mmpcbd_cofile = mmp_uint_arr(cbdcofname);
  for(int j = 0;j< cofnum; j++ ){
#pragma omp parallel for num_threads(p_fit_mem) schedule(guided)
   for(size_t k = cbdcoindex[j]; k< cbdcoindex[j+1]; k++){
    unsigned int ind = mmpcbd_cofile.mmpco[k];
    unsigned int mod = mco_map.row_gnum[ind] % GID_ARR_SZ ;
    gid_arr_llist_t* tmp;
    if(mod == 0){
     tmp = mco[ind];
     mco[ind] = malloc(sizeof(gid_arr_llist_t));
     if (mco[ind] == NULL) err(errno,"cdb_kmerf2kmerdb()::mco[ind]") ;
     mco[ind]->next = tmp;
    }
    mco[ind]->gidobj[mod] = j % BIN_SZ ;
    mco_map.row_gnum[ind]++;
    mco_map.row_bin_gnum[(size_t)ind*binnum + j/BIN_SZ]++;
   }
  }
  munmap(mmpcbd_cofile.mmpco, mmpcbd_cofile.fsize);
  for(size_t n=1; n<comp_sz; n++)
   mco_map.row_offset[n] = mco_map.row_offset[n-1] + mco_map.row_gnum[n-1];
  sprintf(mcoindexf,"%s/mco.index.%d",mcodirname,i);
  FILE *arrmco_index_fp = fopen(mcoindexf,"wb");
  if( arrmco_index_fp == NULL) err(errno,"%s",mcoindexf);
  fwrite(mco_map.row_offset,sizeof(size_t),comp_sz,arrmco_index_fp);
  fwrite(mco_map.row_bin_gnum,sizeof(unsigned int),(size_t)comp_sz*binnum, arrmco_index_fp);
  fclose(arrmco_index_fp);
  sprintf(mcofname,"%s/mco.%d",mcodirname,i);
  int arrmco_fp = open(mcofname,O_RDWR|O_CREAT,0600) ;
  if (arrmco_fp == -1) err(errno,"cdb_kmerf2kmerdb()::%s",mcofname);
  size_t mco_comp_fsize = sizeof(gidobj_t)*(mco_map.row_offset[comp_sz-1] + mco_map.row_gnum[comp_sz-1]) ;
  if(ftruncate(arrmco_fp,mco_comp_fsize) == -1) err(errno,"cdb_kmerf2kmerdb()::ftruncate");
  gidobj_t* mco_mmpf = mmap(NULL,mco_comp_fsize,PROT_WRITE,MAP_SHARED,arrmco_fp,0);
  close(arrmco_fp);
#pragma omp parallel for num_threads(p_fit_mem) schedule(guided)
  for(size_t s = 0; s< comp_sz ; s++ ){
   if( mco_map.row_gnum[s] == 0 ) continue;
    gid_arr_llist_t *tmpblk;
    gidobj_t* current_blkpos_mapin_arrmco = mco_mmpf + mco_map.row_offset[s] + mco_map.row_gnum[s] ;
    int blk_num = mco_map.row_gnum[s]/GID_ARR_SZ;
    int remainder = mco_map.row_gnum[s] % GID_ARR_SZ;
    if( remainder > 0 ) blk_num+=1;
    int blk_len;
    for(int blk = 0; blk < blk_num; blk++){
     if((blk==0) && (remainder > 0) ) blk_len = remainder;
     else blk_len = GID_ARR_SZ;
     current_blkpos_mapin_arrmco -= blk_len;
     memcpy(current_blkpos_mapin_arrmco, mco[s]->gidobj, blk_len * sizeof(gidobj_t));
     tmpblk = mco[s];
     mco[s] = mco[s]->next;
     free(tmpblk);
    }
  }
  if ( msync( mco_mmpf, mco_comp_fsize, MS_ASYNC ) < 0 )
          err(errno,"cdb_kmerf2kmerdb()::msync failed");
   munmap(mco_mmpf,mco_comp_fsize);
  fclose(cbdfp);
  fclose(cbdindexfp);
 }
  free(mco_map.row_bin_gnum);
  free(mco_map.row_gnum);
  free(mco_map.row_offset);
  free(cbdcoindex);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x548, %rsp            # imm = 0x548
movl %ecx, %ebp
movl %edx, %ebx
movq %rsi, 0x88(%rsp)
movq %rdi, %r12
cvtsi2sd %edx, %xmm0
mulsd 0x11552(%rip), %xmm0    # 0x15008
callq 0x3490
cvttsd2si %xmm0, %eax
cltq
movq %rax, 0x38(%rsp)
movq %rax, %rdi
shlq $0x1e, %rdi
movq %rdi, 0x80(%rsp)
callq 0x3340
movq %rax, 0x10(%rsp)
movl $0x40000000, %edi       # imm = 0x40000000
callq 0x3340
movq %rax, %r15
movl $0x80000000, %edi       # imm = 0x80000000
callq 0x3340
movq %rax, %r13
movq $0x0, (%rax)
movl %ebx, 0x5c(%rsp)
movslq %ebx, %rbx
leaq 0x8(,%rbx,8), %rdi
callq 0x3340
movq %rax, 0x30(%rsp)
movl $0x80000000, %edi       # imm = 0x80000000
callq 0x3340
movq %rax, 0x20(%rsp)
movl %ebp, 0x58(%rsp)
testl %ebp, %ebp
movq %r15, %rbp
je 0x4018
movq 0x38(%rsp), %rax
shlq $0x1c, %rax
movq %rax, 0x68(%rsp)
leaq 0x1(%rbx), %rax
movq %rax, 0x78(%rsp)
movl %ebx, %eax
movq %rax, 0xa8(%rsp)
leaq 0x140(%rsp), %r15
xorl %r14d, %r14d
movq %r12, 0x70(%rsp)
movq %rbp, 0x50(%rsp)
movq %r13, 0x60(%rsp)
movq 0x10(%rsp), %rdi
xorl %esi, %esi
movq 0x80(%rsp), %rdx
callq 0x3210
movl $0x40000000, %edx       # imm = 0x40000000
movq %rbp, %rdi
xorl %esi, %esi
callq 0x3210
movq %r15, %rdi
leaq 0x114df(%rip), %rsi     # 0x1507e
movq 0x88(%rsp), %rbx
movq %rbx, %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x3430
leaq 0x240(%rsp), %r12
movq %r12, %rdi
leaq 0x114cb(%rip), %rsi     # 0x15091
movq %rbx, %rdx
movl %r14d, 0xc(%rsp)
movl %r14d, %ecx
xorl %eax, %eax
callq 0x3430
movq %r12, %rdi
leaq 0x11485(%rip), %rbx     # 0x15067
movq %rbx, %rsi
callq 0x33a0
movq %rax, 0x98(%rsp)
testq %rax, %rax
je 0x4062
movq %r15, %rdi
movq %rbx, %rsi
callq 0x33a0
testq %rax, %rax
je 0x407a
movl $0x8, %esi
movq 0x30(%rsp), %rdi
movq 0x78(%rsp), %rdx
movq %rax, 0x90(%rsp)
movq %rax, %rcx
callq 0x30c0
leaq 0x240(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x3380
movl %eax, %ebp
movl %eax, %r12d
shrl $0x1f, %r12d
callq 0x3060
movq %rax, %r14
movl (%rax), %edi
callq 0x3470
movl %r12d, %edi
leaq 0x1154b(%rip), %rsi     # 0x151af
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x46d6
movl %ebp, %edi
leaq 0xb0(%rsp), %rsi
callq 0x34a0
movq 0xe0(%rsp), %rsi
xorl %edi, %edi
movq %rsi, 0xa0(%rsp)
movl $0x1, %edx
movl $0x2, %ecx
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x3150
movq %rax, %r15
xorl %r12d, %r12d
cmpq $-0x1, %rax
sete %r12b
movq %r14, (%rsp)
movl (%r14), %edi
callq 0x3470
movl %r12d, %edi
leaq 0x114f7(%rip), %rsi     # 0x151c2
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x46d6
movl %ebp, %edi
callq 0x3220
movq %r15, %rbx
cmpl $0x0, 0x5c(%rsp)
movq 0x38(%rsp), %rcx
movq 0x50(%rsp), %rbp
movq 0x20(%rsp), %rsi
jle 0x3dc3
xorl %edx, %edx
movq 0x30(%rsp), %rax
movq (%rax,%rdx,8), %r14
movq %rdx, 0x18(%rsp)
movq 0x8(%rax,%rdx,8), %r8
cmpq %r8, %r14
jae 0x3dad
movq 0x18(%rsp), %rax
movzwl %ax, %r9d
shrl $0x10, %eax
movq 0x10(%rsp), %rdx
leaq (%rdx,%rax,4), %rdx
movq %r8, 0x28(%rsp)
movl %r9d, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movl (%rbx,%r14,4), %r12d
movl (%rbp,%r12,4), %r13d
movq %r13, %r15
andq $0xf, %r15
jne 0x3d8d
movq (%rsi,%r12,8), %rbp
movl $0x48, %edi
callq 0x3340
movq 0x20(%rsp), %rsi
movq %rax, (%rsi,%r12,8)
testq %rax, %rax
je 0x404e
movq %rbp, 0x40(%rax)
movq 0x38(%rsp), %rcx
movq 0x50(%rsp), %rbp
movq 0x28(%rsp), %r8
movl 0x48(%rsp), %r9d
movq 0x40(%rsp), %rdx
movq (%rsi,%r12,8), %rax
movl %r9d, (%rax,%r15,4)
incl %r13d
movl %r13d, (%rbp,%r12,4)
imulq %rcx, %r12
incl (%rdx,%r12,4)
incq %r14
cmpq %r14, %r8
jne 0x3d3e
movq 0x18(%rsp), %rdx
incq %rdx
cmpq 0xa8(%rsp), %rdx
jne 0x3cfe
movq %rbx, %rdi
movq 0xa0(%rsp), %rsi
callq 0x3360
movq 0x60(%rsp), %r13
movq (%r13), %rax
movl $0x1, %ecx
movl -0x4(%rbp,%rcx,4), %edx
addq %rdx, %rax
movq %rax, (%r13,%rcx,8)
incq %rcx
cmpq $0x10000000, %rcx       # imm = 0x10000000
jne 0x3de1
leaq 0x340(%rsp), %r14
movq %r14, %rdi
leaq 0x112b0(%rip), %rsi     # 0x150bb
movq 0x70(%rsp), %rbx
movq %rbx, %rdx
movl 0xc(%rsp), %r15d
movl %r15d, %ecx
xorl %eax, %eax
callq 0x3430
movq %r14, %rdi
leaq 0x1123e(%rip), %rsi     # 0x1506a
callq 0x33a0
testq %rax, %rax
je 0x4092
movq %rax, %r12
movl $0x8, %esi
movl $0x10000000, %edx       # imm = 0x10000000
movq %r13, %rdi
movq %rax, %rcx
callq 0x3450
movl $0x4, %esi
movq 0x10(%rsp), %rdi
movq 0x68(%rsp), %rdx
movq %r12, %rcx
callq 0x3450
movq %r12, %rdi
callq 0x3100
leaq 0x440(%rsp), %r14
movq %r14, %rdi
leaq 0x11248(%rip), %rsi     # 0x150cb
movq %rbx, %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x3430
movq %r14, %rdi
movl $0x42, %esi
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x3380
cmpl $-0x1, %eax
je 0x40a9
movl %eax, %r12d
movl 0x3ffffffc(%rbp), %ebx
addq 0x7ffffff8(%r13), %rbx
shlq $0x2, %rbx
movl %eax, %edi
movq %rbx, %rsi
callq 0x31e0
cmpl $-0x1, %eax
je 0x40c5
xorl %edi, %edi
movq %rbx, 0x18(%rsp)
movq %rbx, %rsi
movl $0x2, %edx
movl $0x1, %ecx
movl %r12d, %r8d
xorl %r9d, %r9d
callq 0x3150
movq %rax, %r14
movl %r12d, %edi
callq 0x3220
xorl %esi, %esi
movq 0x20(%rsp), %rdx
movq %r14, 0x40(%rsp)
movl (%rbp,%rsi,4), %eax
testq %rax, %rax
je 0x3fab
movl %eax, %ecx
shrl $0x4, %ecx
xorl %r15d, %r15d
movl %eax, %edi
andl $0xf, %edi
movl %edi, 0x28(%rsp)
setne %r15b
addl %ecx, %r15d
je 0x3fab
movq (%r13,%rsi,8), %rcx
leaq (%r14,%rcx,4), %rcx
leaq (%rcx,%rax,4), %r12
movl 0x28(%rsp), %ebx
movq %rsi, 0x48(%rsp)
movq (%rdx,%rsi,8), %r14
xorl %r13d, %r13d
movq %r14, %rbp
testl %r13d, %r13d
movl $0x10, %edx
cmovel %ebx, %edx
cmpl $0x0, 0x28(%rsp)
movl $0x10, %eax
cmovel %eax, %edx
shll $0x2, %edx
subq %rdx, %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x32f0
movq 0x40(%r14), %r14
movq %rbp, %rdi
callq 0x3040
incl %r13d
cmpl %r13d, %r15d
jne 0x3f4e
movq 0x20(%rsp), %rdx
movq 0x48(%rsp), %rsi
movq %r14, (%rdx,%rsi,8)
movq 0x50(%rsp), %rbp
movq 0x60(%rsp), %r13
movq 0x40(%rsp), %r14
incq %rsi
cmpq $0x10000000, %rsi       # imm = 0x10000000
jne 0x3f0a
movq %r14, %rdi
movq 0x18(%rsp), %rbx
movq %rbx, %rsi
movl $0x1, %edx
callq 0x3120
testl %eax, %eax
js 0x40d4
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3360
movq 0x98(%rsp), %rdi
callq 0x3100
movq 0x90(%rsp), %rdi
callq 0x3100
movl 0xc(%rsp), %r14d
incl %r14d
cmpl 0x58(%rsp), %r14d
leaq 0x140(%rsp), %r15
jne 0x3b72
movq 0x10(%rsp), %rdi
callq 0x3040
movq %rbp, %rdi
callq 0x3040
movq %r13, %rdi
callq 0x3040
movq 0x30(%rsp), %rdi
callq 0x3040
addq $0x548, %rsp            # imm = 0x548
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %rax
movl (%rax), %edi
leaq 0x11043(%rip), %rsi     # 0x1509e
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x12306(%rip), %rsi     # 0x16376
leaq 0x240(%rsp), %rdx
jmp 0x40be
callq 0x3060
movl (%rax), %edi
leaq 0x122ee(%rip), %rsi     # 0x16376
leaq 0x140(%rsp), %rdx
jmp 0x40be
movq (%rsp), %rax
movl (%rax), %edi
leaq 0x122d7(%rip), %rsi     # 0x16376
leaq 0x340(%rsp), %rdx
jmp 0x40be
movq (%rsp), %rax
movl (%rax), %edi
leaq 0x1101f(%rip), %rsi     # 0x150d5
leaq 0x440(%rsp), %rdx
xorl %eax, %eax
callq 0x3330
movq (%rsp), %rax
movl (%rax), %edi
leaq 0x1101a(%rip), %rsi     # 0x150ec
jmp 0x405b
movq (%rsp), %rax
movl (%rax), %edi
leaq 0x11029(%rip), %rsi     # 0x1510a
jmp 0x405b