// File: /workspace/github_c_makefile_star2_O2/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 $0x4b8, %rsp            # imm = 0x4B8
movl %ecx, 0x54(%rsp)
movl %edx, %ebp
movq %rsi, 0x80(%rsp)
movq %rdi, 0x68(%rsp)
cvtsi2sd %edx, %xmm0
mulsd 0x1054b(%rip), %xmm0    # 0x14008
callq 0x3490
cvttsd2si %xmm0, %eax
cltq
movq %rax, %rcx
shlq $0x1c, %rcx
movq %rcx, 0x60(%rsp)
movq %rax, 0x58(%rsp)
movq %rax, %rdi
shlq $0x1e, %rdi
movq %rdi, 0x78(%rsp)
callq 0x3340
movq %rax, 0x18(%rsp)
movl $0x40000000, %edi       # imm = 0x40000000
callq 0x3340
movq %rax, 0x8(%rsp)
movl $0x80000000, %edi       # imm = 0x80000000
callq 0x3340
movq %rax, %rbx
andq $0x0, (%rax)
movslq %ebp, %r14
leaq 0x1(%r14), %rax
movq %rax, 0x70(%rsp)
leaq 0x8(,%r14,8), %rdi
callq 0x3340
movq %rax, 0x30(%rsp)
movl $0x80000000, %edi       # imm = 0x80000000
callq 0x3340
xorl %ecx, %ecx
testl %r14d, %r14d
cmovlel %ecx, %r14d
movq %r14, 0xa0(%rsp)
leaq 0xb0(%rsp), %r14
xorl %ebp, %ebp
movq %rbx, 0x20(%rsp)
movq %rax, 0x48(%rsp)
cmpl 0x54(%rsp), %ebp
je 0x3f37
movq 0x18(%rsp), %rdi
xorl %esi, %esi
movq 0x78(%rsp), %rdx
callq 0x3210
movl $0x40000000, %edx       # imm = 0x40000000
movq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x3210
movq %r14, %rdi
leaq 0x104ec(%rip), %rsi     # 0x1407e
movq 0x80(%rsp), %rbx
movq %rbx, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3430
leaq 0x1b0(%rsp), %r15
movq %r15, %rdi
leaq 0x104d9(%rip), %rsi     # 0x14091
movq %rbx, %rdx
movl %ebp, 0x14(%rsp)
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3430
movq %r15, %rdi
leaq 0x10495(%rip), %rbx     # 0x14067
movq %rbx, %rsi
callq 0x33a0
movq %rax, 0x90(%rsp)
testq %rax, %rax
je 0x3f84
movq %r14, %rdi
movq %rbx, %rsi
callq 0x33a0
testq %rax, %rax
je 0x3f9c
movq 0x30(%rsp), %rdi
pushq $0x8
popq %rsi
movq 0x70(%rsp), %rdx
movq %rax, 0x88(%rsp)
movq %rax, %rcx
callq 0x30c0
leaq 0x1b0(%rsp), %rdi
callq 0x39cc
movq %rax, 0x98(%rsp)
movq %rdx, %rbx
xorl %eax, %eax
movq 0x58(%rsp), %rcx
cmpq 0xa0(%rsp), %rax
je 0x3d04
movq 0x30(%rsp), %rdx
movq (%rdx,%rax,8), %r15
movq 0x8(%rdx,%rax,8), %rsi
leaq 0x1(%rax), %rdx
movq %rdx, 0xa8(%rsp)
movzwl %ax, %r8d
shrl $0x10, %eax
movq 0x18(%rsp), %rdx
leaq (%rdx,%rax,4), %r9
movq 0x8(%rsp), %rax
movq 0x48(%rsp), %rdx
movq %rsi, 0x40(%rsp)
movl %r8d, 0x28(%rsp)
movq %r9, 0x38(%rsp)
cmpq %rsi, %r15
jae 0x3cf7
movl (%rbx,%r15,4), %r14d
movl (%rax,%r14,4), %ebp
movq %rbp, %r13
movq (%rdx,%r14,8), %r12
andq $0xf, %r13
jne 0x3ce0
pushq $0x48
popq %rdi
callq 0x3340
movq 0x48(%rsp), %rdx
movq %rax, (%rdx,%r14,8)
testq %rax, %rax
je 0x3f6f
movq %r12, 0x40(%rax)
movq %rax, %r12
movq 0x58(%rsp), %rcx
movq 0x8(%rsp), %rax
movq 0x40(%rsp), %rsi
movl 0x28(%rsp), %r8d
movq 0x38(%rsp), %r9
movl %r8d, (%r12,%r13,4)
incl %ebp
movl %ebp, (%rax,%r14,4)
imulq %rcx, %r14
incl (%r9,%r14,4)
incq %r15
jmp 0x3c8c
movq 0xa8(%rsp), %rax
jmp 0x3c3b
movq %rbx, %rdi
movq 0x98(%rsp), %rsi
callq 0x3360
xorl %eax, %eax
movq 0x68(%rsp), %rbx
cmpq $0xfffffff, %rax        # imm = 0xFFFFFFF
je 0x3d3e
movq 0x8(%rsp), %rcx
movl (%rcx,%rax,4), %ecx
movq 0x20(%rsp), %rdx
addq (%rdx,%rax,8), %rcx
movq %rcx, 0x8(%rdx,%rax,8)
incq %rax
jmp 0x3d1b
leaq 0x2b0(%rsp), %r14
movq %r14, %rdi
leaq 0x1036b(%rip), %rsi     # 0x140bb
movq %rbx, %rdx
movl 0x14(%rsp), %ebp
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3430
movq %r14, %rdi
leaq 0x10300(%rip), %rsi     # 0x1406a
callq 0x33a0
testq %rax, %rax
je 0x3fd4
movq %rax, %r13
movl $0x10000000, %edx       # imm = 0x10000000
movq 0x20(%rsp), %r14
movq %r14, %rdi
pushq $0x8
popq %rsi
movq %rax, %rcx
callq 0x3450
movq 0x18(%rsp), %rdi
pushq $0x4
popq %rsi
movq 0x60(%rsp), %rdx
movq %r13, %rcx
callq 0x3450
movq %r13, %rdi
callq 0x3100
leaq 0x3b0(%rsp), %r15
movq %r15, %rdi
leaq 0x10309(%rip), %rsi     # 0x140cb
movq %rbx, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3430
movq %r15, %rdi
pushq $0x42
popq %rsi
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x3380
cmpl $-0x1, %eax
je 0x3fec
movl %eax, %r13d
movq 0x8(%rsp), %rax
movl 0x3ffffffc(%rax), %ebx
addq 0x7ffffff8(%r14), %rbx
shlq $0x2, %rbx
movl %r13d, %edi
movq %rbx, %rsi
callq 0x31e0
cmpl $-0x1, %eax
je 0x3fb4
xorl %edi, %edi
movq %rbx, 0x38(%rsp)
movq %rbx, %rsi
pushq $0x2
popq %rdx
pushq $0x1
popq %rcx
movl %r13d, %r8d
xorl %r9d, %r9d
callq 0x3150
movq %rax, 0x28(%rsp)
movl %r13d, %edi
callq 0x3220
xorl %ebp, %ebp
cmpq $0x10000000, %rbp       # imm = 0x10000000
movq 0x8(%rsp), %rax
je 0x3eda
movl (%rax,%rbp,4), %r15d
testq %r15, %r15
je 0x3ed2
movq 0x20(%rsp), %rax
movq (%rax,%rbp,8), %rax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax,4), %rax
leaq (%rax,%r15,4), %r13
movl %r15d, %eax
shrl $0x4, %eax
andl $0xf, %r15d
cmpl $0x1, %r15d
sbbl $-0x1, %eax
movl %eax, 0x40(%rsp)
xorl %r14d, %r14d
cmpl %r14d, 0x40(%rsp)
je 0x3ed2
testl %r14d, %r14d
pushq $0x10
popq %rax
movl %eax, %edx
cmovel %r15d, %edx
testq %r15, %r15
cmovel %eax, %edx
shll $0x2, %edx
subq %rdx, %r13
movq 0x48(%rsp), %rbx
movq (%rbx,%rbp,8), %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0x32f0
movq 0x40(%r12), %rax
movq %rax, (%rbx,%rbp,8)
movq %r12, %rdi
callq 0x3040
incl %r14d
jmp 0x3e89
incq %rbp
jmp 0x3e40
movq 0x28(%rsp), %r14
movq %r14, %rdi
movq 0x38(%rsp), %rbx
movq %rbx, %rsi
pushq $0x1
popq %rdx
callq 0x3120
testl %eax, %eax
js 0x3fc4
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3360
movq 0x90(%rsp), %rdi
callq 0x3100
movq 0x88(%rsp), %rdi
callq 0x3100
movl 0x14(%rsp), %ebp
incl %ebp
movq 0x20(%rsp), %rbx
leaq 0xb0(%rsp), %r14
jmp 0x3b5c
movq 0x18(%rsp), %rdi
callq 0x3040
movq 0x8(%rsp), %rdi
callq 0x3040
movq %rbx, %rdi
callq 0x3040
movq 0x30(%rsp), %rdi
callq 0x3040
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3060
movl (%rax), %edi
leaq 0x10121(%rip), %rsi     # 0x1409e
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x113e4(%rip), %rsi     # 0x15376
leaq 0x1b0(%rsp), %rdx
jmp 0x4002
callq 0x3060
movl (%rax), %edi
leaq 0x113cc(%rip), %rsi     # 0x15376
leaq 0xb0(%rsp), %rdx
jmp 0x4002
callq 0x3060
movl (%rax), %edi
leaq 0x1012a(%rip), %rsi     # 0x140ec
jmp 0x3f7d
callq 0x3060
movl (%rax), %edi
leaq 0x10138(%rip), %rsi     # 0x1410a
jmp 0x3f7d
callq 0x3060
movl (%rax), %edi
leaq 0x11394(%rip), %rsi     # 0x15376
leaq 0x2b0(%rsp), %rdx
jmp 0x4002
callq 0x3060
movl (%rax), %edi
leaq 0x100db(%rip), %rsi     # 0x140d5
leaq 0x3b0(%rsp), %rdx
xorl %eax, %eax
callq 0x3330